File size: 606 Bytes
71d239c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | """Take a question plus relevant documents and write the final answer."""
from openai import OpenAI
import config
from retrieve import retrieve
client = OpenAI(api_key=config.OPENAI_API_KEY)
def answer(question):
"""Find relevant docs, then ask the model to answer using them."""
context = "\n\n".join(retrieve(question))
prompt = f"Use this context to answer.\n\nContext:\n{context}\n\nQuestion: {question}"
resp = client.chat.completions.create(
model=config.CHAT_MODEL,
messages=[{"role": "user", "content": prompt}],
)
return resp.choices[0].message.content
|