Spaces:
Running
Running
use Messages API (OpenAI)
Browse files
rag.py
CHANGED
@@ -35,27 +35,28 @@ class RAG:
|
|
35 |
|
36 |
def predict(self, instruction, context, model_parameters):
|
37 |
|
38 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|