MEOW commited on
Commit
96bf947
·
unverified ·
1 Parent(s): ad9994a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -11,34 +11,19 @@ from llama_index.core.node_parser import SentenceSplitter
11
  logging.basicConfig(level=logging.INFO)
12
  logger = logging.getLogger(__name__)
13
 
14
- # Assuming xAI provides their endpoint for generating text
15
- XAI_API_URL = "https://api.x.ai/v1/chat/completions"
16
 
17
- # Custom LLM class for xAI
18
- class XAILLM:
19
- def __init__(self):
20
- self.api_key = os.getenv('XAI_API_KEY')
21
- if not self.api_key:
22
- raise ValueError("XAI_API_KEY environment variable is not set")
23
 
24
- def complete(self, prompt, **kwargs):
25
- headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
26
- data = {
27
- "model": "grok-beta", # Adjust model name as per xAI's API
28
- "messages": [{"role": "user", "content": prompt}],
29
- "stream": True # or True if you want streaming responses
30
- }
31
- response = requests.post(XAI_API_URL, headers=headers, json=data)
32
- response.raise_for_status()
33
- return response.json()['choices'][0]['message']['content'] # Adjust based on actual API response structure
34
-
35
- Settings.llm = XAILLM()
36
-
37
- # Custom Embedding class for xAI
38
  class XAIEmbedding:
39
  def __init__(self):
40
- self.api_key = os.getenv('XAI_API_KEY')
41
- self.model_id = "v1" # Adjust as per xAI's model ID
42
 
43
  def get_text_embedding(self, text):
44
  headers = {
@@ -49,20 +34,38 @@ class XAIEmbedding:
49
  "input": text,
50
  "model": self.model_id
51
  }
52
- response = requests.post(XAI_API_URL, headers=headers, json=data)
53
  response.raise_for_status()
54
  return response.json()['data'][0]['embedding']
55
 
56
  def get_text_embedding_batch(self, texts, show_progress=False):
57
- # If show_progress is set to True, you could implement progress reporting here.
58
- # However, since the requests library doesn't inherently support this, we'll just print if it's true.
59
  if show_progress:
60
  print("Generating embeddings for batch of texts...")
61
-
62
  return [self.get_text_embedding(text) for text in texts]
63
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  Settings.embed_model = XAIEmbedding()
65
  Settings.text_splitter = SentenceSplitter(chunk_size=400)
 
66
 
67
  # Ensure GPU usage
68
  #if torch.cuda.is_available():
 
11
  logging.basicConfig(level=logging.INFO)
12
  logger = logging.getLogger(__name__)
13
 
14
+ # xAI API Base URL (Adjust as needed)
15
+ XAI_API_BASE = "https://api.x.ai/"
16
 
17
+ # Construct specific endpoints
18
+ XAI_API_KEY = os.getenv('XAI_API_KEY')
19
+ XAI_API_URL_EMBEDDINGS = f"{XAI_API_BASE}v1/embeddings"
20
+ XAI_API_URL_CHAT_COMPLETIONS = f"{XAI_API_BASE}v1/chat/completions"
 
 
21
 
22
+ # Custom embedding class for xAI
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  class XAIEmbedding:
24
  def __init__(self):
25
+ self.api_key = XAI_API_KEY
26
+ self.model_id = "v1" # Adjust as per xAI's model ID for embeddings
27
 
28
  def get_text_embedding(self, text):
29
  headers = {
 
34
  "input": text,
35
  "model": self.model_id
36
  }
37
+ response = requests.post(XAI_API_URL_EMBEDDINGS, headers=headers, json=data)
38
  response.raise_for_status()
39
  return response.json()['data'][0]['embedding']
40
 
41
  def get_text_embedding_batch(self, texts, show_progress=False):
 
 
42
  if show_progress:
43
  print("Generating embeddings for batch of texts...")
 
44
  return [self.get_text_embedding(text) for text in texts]
45
+
46
+ # Custom LLM class for xAI
47
+ class XAILLM:
48
+ def __init__(self):
49
+ self.api_key = XAI_API_KEY
50
+ self.model = "grok" # Adjust as per xAI's model ID for text generation
51
+
52
+ def complete(self, prompt, **kwargs):
53
+ headers = {
54
+ "Authorization": f"Bearer {self.api_key}",
55
+ "Content-Type": "application/json"
56
+ }
57
+ data = {
58
+ "model": self.model,
59
+ "messages": [{"role": "user", "content": prompt}]
60
+ }
61
+ response = requests.post(XAI_API_URL_CHAT_COMPLETIONS, headers=headers, json=data)
62
+ response.raise_for_status()
63
+ return response.json()['choices'][0]['message']['content']
64
+
65
+ # Configure settings
66
  Settings.embed_model = XAIEmbedding()
67
  Settings.text_splitter = SentenceSplitter(chunk_size=400)
68
+ Settings.llm = XAILLM()
69
 
70
  # Ensure GPU usage
71
  #if torch.cuda.is_available():