File size: 908 Bytes
0753d2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import openai
from typing import List

class QASystem:
    def __init__(self, api_key: str):
        openai.api_key = api_key
        
    def generate_answer(self, question: str, context: List[str]) -> str:
        prompt = f"""Based on the context provided below, answer the question.
        If the answer is not in the context, respond with "The answer is not in the provided context."

        Context:
        {' '.join(context)}

        Question: {question}
        """
        
        response = openai.chat.completions.create(  # Updated line
            model="gpt-4",
            messages=[
                {"role": "system", "content": "You are an assistant answering questions based on the provided context."},
                {"role": "user", "content": prompt}
            ],
            temperature=0,
            max_tokens=500
        )
        return response.choices[0].message.content