MEOW commited on
Commit
be5be6f
·
unverified ·
1 Parent(s): 2364404

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -5,28 +5,34 @@ import requests
5
  from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, StorageContext
6
  from llama_index.vector_stores.milvus import MilvusVectorStore
7
  from llama_index.core.node_parser import SentenceSplitter
8
- from requests.exceptions import RequestException # Timeout
 
 
 
 
9
 
10
- # Custom XAIService for embedding and chat completions
11
  class XAIService:
12
- def __init__(self, base_url: str = "https://api.x.ai/v1"):
13
- self.api_key = os.getenv('XAI_API_KEY')
14
- if not self.api_key:
15
- raise ValueError("XAI_API_KEY environment variable is not set")
 
 
 
16
  self.base_url = base_url
17
 
18
- def get_embedding(self, text):
19
  endpoint = f"{self.base_url}/embeddings"
20
- payload = {"input": text}
21
- response = requests.post(endpoint, headers={
22
  "Authorization": f"Bearer {self.api_key}",
23
  "Content-Type": "application/json",
24
  "Accept": "application/json"
25
- }, json=payload)
 
26
  response.raise_for_status()
27
  return response.json()['data'][0]['embedding']
28
 
29
- def get_response(self, user_message, system_message="You are a helpful AI assistant.", model="grok-beta", temperature=0.7):
30
  endpoint = f"{self.base_url}/chat/completions"
31
  payload = {
32
  "messages": [
@@ -37,24 +43,20 @@ class XAIService:
37
  "temperature": temperature,
38
  "stream": False
39
  }
40
- response = requests.post(endpoint, headers={
41
  "Authorization": f"Bearer {self.api_key}",
42
  "Content-Type": "application/json",
43
  "Accept": "application/json"
44
- }, json=payload)
 
45
  response.raise_for_status()
46
  return response.json()['choices'][0]['message']['content']
47
-
48
- # Configure xAI API settings
49
- XAI_API_KEY = os.getenv('XAI_API_KEY')
50
-
51
- # Initialize XAIService
52
- xai_service = XAIService(XAI_API_KEY)
53
 
54
  # Configure settings
55
- Settings.embed_model = xai_service.get_embedding # Now using xAI's embedding service
 
 
56
  Settings.text_splitter = SentenceSplitter(chunk_size=400)
57
- Settings.llm = xai_service # Use XAIService for LLM interaction
58
 
59
  index = None
60
  query_engine = None
 
5
  from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, StorageContext
6
  from llama_index.vector_stores.milvus import MilvusVectorStore
7
  from llama_index.core.node_parser import SentenceSplitter
8
+ from requests.exceptions import RequestException
9
+ from typing import List
10
+
11
+ # Configure xAI API settings
12
+ XAI_API_KEY = os.getenv('XAI_API_KEY')
13
 
 
14
  class XAIService:
15
+ def __init__(self, base_url: str = "https://api.x.ai/v1", api_key: str = None):
16
+ if api_key is None:
17
+ self.api_key = os.getenv('XAI_API_KEY')
18
+ if not self.api_key:
19
+ raise ValueError("XAI_API_KEY environment variable is not set or api_key not provided")
20
+ else:
21
+ self.api_key = api_key
22
  self.base_url = base_url
23
 
24
+ def get_embedding(self, text: str) -> List[float]:
25
  endpoint = f"{self.base_url}/embeddings"
26
+ headers = {
 
27
  "Authorization": f"Bearer {self.api_key}",
28
  "Content-Type": "application/json",
29
  "Accept": "application/json"
30
+ }
31
+ response = requests.post(endpoint, headers=headers, json={"input": text})
32
  response.raise_for_status()
33
  return response.json()['data'][0]['embedding']
34
 
35
+ def get_response(self, user_message: str, system_message: str = "You are an AI with access to external documents.", model: str = "grok-beta", temperature: float = 0.7) -> str:
36
  endpoint = f"{self.base_url}/chat/completions"
37
  payload = {
38
  "messages": [
 
43
  "temperature": temperature,
44
  "stream": False
45
  }
46
+ headers = {
47
  "Authorization": f"Bearer {self.api_key}",
48
  "Content-Type": "application/json",
49
  "Accept": "application/json"
50
+ }
51
+ response = requests.post(endpoint, headers=headers, json=payload)
52
  response.raise_for_status()
53
  return response.json()['choices'][0]['message']['content']
 
 
 
 
 
 
54
 
55
  # Configure settings
56
+ xai_service = XAIService(XAI_API_KEY)
57
+ Settings.llm = xai_service
58
+ Settings.embed_model = lambda text: xai_service.get_embedding(text) # This now directly uses XAIService's embedding method
59
  Settings.text_splitter = SentenceSplitter(chunk_size=400)
 
60
 
61
  index = None
62
  query_engine = None