Spaces:
Sleeping
Sleeping
yash bhaskar
commited on
Commit
·
a639959
1
Parent(s):
26a3a6e
Adding Answer Generation Pipeline
Browse files
AnswerGeneration/getAnswer.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from together import Together
|
| 4 |
+
|
| 5 |
+
def generate_answer_withContext(question, context):
|
| 6 |
+
with open('config.json', 'r') as file:
|
| 7 |
+
config = json.load(file)
|
| 8 |
+
|
| 9 |
+
together_ai_key = config.get("TOGETHER_AI")
|
| 10 |
+
|
| 11 |
+
client = Together(api_key=together_ai_key)
|
| 12 |
+
|
| 13 |
+
prompt = f"""Consider the context and generate a brief 1-2 line answer to the question. Output only the answer.
|
| 14 |
+
|
| 15 |
+
Context: {context}
|
| 16 |
+
|
| 17 |
+
Question: {question}
|
| 18 |
+
"""
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
# model="meta-llama/Llama-3-8b-chat-hf",
|
| 21 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
| 22 |
+
messages=[{"role": "user", "content": prompt}],
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
return response.choices[0].message.content
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def generate_answer_zeroShot(question):
|
| 29 |
+
with open('config.json', 'r') as file:
|
| 30 |
+
config = json.load(file)
|
| 31 |
+
|
| 32 |
+
together_ai_key = config.get("TOGETHER_AI")
|
| 33 |
+
|
| 34 |
+
client = Together(api_key=together_ai_key)
|
| 35 |
+
|
| 36 |
+
prompt = f"""Answer the following question:
|
| 37 |
+
|
| 38 |
+
Question: {question}
|
| 39 |
+
"""
|
| 40 |
+
response = client.chat.completions.create(
|
| 41 |
+
model="meta-llama/Llama-3-8b-chat-hf",
|
| 42 |
+
messages=[{"role": "user", "content": prompt}],
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return response.choices[0].message.content
|