-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate.py
More file actions
38 lines (31 loc) · 1.04 KB
/
Copy pathgenerate.py
File metadata and controls
38 lines (31 loc) · 1.04 KB
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
29
30
31
32
33
34
35
36
37
38
import os
import openai
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Retrieve the OpenAI API key from the environment variable
openai.api_key = os.getenv('OPENAI_API_KEY')
def generate_response(retrieved_texts, query, max_tokens=150):
"""
Generates a response based on the retrieved texts and query.
Args:
retrieved_texts (list): List of retrieved text strings.
query (str): Query string.
max_tokens (int): Maximum number of tokens for the response.
Returns:
str: Generated response.
"""
context = "\n".join(retrieved_texts)
prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
max_tokens=max_tokens,
n=1,
stop=None,
temperature=0.5,
)
return response.choices[0].message['content']