File size: 1,082 Bytes
bac8f2c
 
2ecdf10
5ec3d66
bac8f2c
 
89c134b
930d59b
89c134b
 
bac8f2c
 
89c134b
930d59b
89c134b
 
 
 
 
 
 
 
 
 
 
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
import openai

openai.api_key = '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'
model = "gpt-3.5-turbo-1106"

def get_keywords(question):
    prompt = f"Extract 10 keywords from the following question, including synonyms. Use only lowercase letters:\n\n{question}"
    response = openai.chat.completions.create(model=model, prompt=prompt, max_tokens=60, temperature=0)
    keywords = response.choices[0].text.strip().split(', ')
    return keywords

def answer_question(chunk, question):
    prompt = f"Based on the following information, what is the answer to this question?\n\nText:\n{chunk}\n\nQuestion:\n{question}"
    response = openai.chat.completions.create(model=model, prompt=prompt, max_tokens=150, temperature=0)
    answer = response.choices[0].text.strip()
    return answer

# Example usage
question = "What are the key aspects of climate change?"
keywords = get_keywords(question)
print("Keywords:", keywords)

chunk = "Climate change is a long-term change in the average weather patterns..."
answer = answer_question(chunk, question)
print("Answer:", answer)