jgrivolla commited on
Commit
c42abf4
·
verified ·
1 Parent(s): 4ea695e

use Messages API (OpenAI)

Browse files
Files changed (1) hide show
  1. rag.py +20 -19
rag.py CHANGED
@@ -35,27 +35,28 @@ class RAG:
35
 
36
  def predict(self, instruction, context, model_parameters):
37
 
38
- api_key = os.getenv("HF_TOKEN")
39
-
40
-
41
- headers = {
42
- "Accept" : "application/json",
43
- "Authorization": f"Bearer {api_key}",
44
- "Content-Type": "application/json"
45
- }
46
-
47
- query = f"### Instruction\n{instruction}\n\n### Context\n{context}\n\n### Answer\n "
48
- #prompt = "You are a helpful assistant. Answer the question using only the context you are provided with. If it is not possible to do it with the context, just say 'I can't answer'. <|endoftext|>"
49
-
50
-
51
- payload = {
52
- "inputs": query,
53
- "parameters": model_parameters
54
- }
55
 
56
- response = requests.post(self.model_name, headers=headers, json=payload)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- return response.json()[0]["generated_text"].split("###")[-1][8:]
59
 
60
  def beautiful_context(self, docs):
61
 
 
35
 
36
  def predict(self, instruction, context, model_parameters):
37
 
38
+ from openai import OpenAI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ # init the client but point it to TGI
41
+ client = OpenAI(
42
+ base_url=os.getenv("MODEL")+ "/v1/",
43
+ api_key=os.getenv("HF_TOKEN")
44
+ )
45
+
46
+ query = f"{context}\n\n{instruction}"
47
+ #sys_prompt = "You are a helpful assistant. Answer the question using only the context you are provided with. If it is not possible to do it with the context, just say 'I can't answer'. <|endoftext|>"
48
+
49
+ chat_completion = client.chat.completions.create(
50
+ model="tgi",
51
+ messages=[
52
+ #{"role": "system", "content": sys_prompt },
53
+ {"role": "user", "content": query}
54
+ ],
55
+ stream=False
56
+ )
57
+
58
+ return(chat_completion)
59
 
 
60
 
61
  def beautiful_context(self, docs):
62