Spaces:
Runtime error
Runtime error
Commit
·
89c134b
1
Parent(s):
50186f3
Update gpt.py
Browse files
gpt.py
CHANGED
@@ -1,100 +1,24 @@
|
|
1 |
import openai
|
2 |
-
import json
|
3 |
|
4 |
model = "gpt-3.5-turbo"
|
5 |
|
6 |
-
|
7 |
def get_keywords(question):
|
8 |
-
prompt = f"
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
response = openai.completions.create(
|
13 |
-
model=model,
|
14 |
-
messages=[
|
15 |
-
{
|
16 |
-
"role": "system",
|
17 |
-
"content": "Vous fournirez toujours 10 mots-clés incluant des synonymes pertinents et explicit des mots de la question d’origine. Les synonymes doivent être des termes juridiques couramment utilisés dans les articles de loi canadienne", },
|
18 |
-
{
|
19 |
-
"role": "user",
|
20 |
-
"content": prompt,
|
21 |
-
},
|
22 |
-
],
|
23 |
-
functions=[
|
24 |
-
{
|
25 |
-
"name": "list_keywords",
|
26 |
-
"description": "Utilisez cette fonction pour donner à l'utilisateur une liste de mots-clés",
|
27 |
-
"parameters": {
|
28 |
-
"type": "object",
|
29 |
-
"properties": {
|
30 |
-
"list": {
|
31 |
-
"type": "array",
|
32 |
-
"items": {"type": "string", "description": "A keyword"},
|
33 |
-
"description": "A list of keywords",
|
34 |
-
}
|
35 |
-
},
|
36 |
-
},
|
37 |
-
"required": ["list"],
|
38 |
-
}
|
39 |
-
],
|
40 |
-
function_call={"name": "list_keywords", "arguments": ["list"]},
|
41 |
-
)
|
42 |
-
|
43 |
-
arguments = response["choices"][0]["message"]["function_call"]["arguments"].lower()
|
44 |
-
keywords = json.loads(arguments)["list"]
|
45 |
-
|
46 |
-
return " ".join(keywords).split(" ")
|
47 |
-
|
48 |
|
49 |
def answer_question(chunk, question):
|
50 |
-
prompt = f""
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
{
|
64 |
-
"role": "system",
|
65 |
-
"content": "Définissez toujours answer_found sur false si la réponse à la question n'a pas été trouvée dans les informations fournies.",
|
66 |
-
},
|
67 |
-
{
|
68 |
-
"role": "user",
|
69 |
-
"content": prompt,
|
70 |
-
},
|
71 |
-
],
|
72 |
-
functions=[
|
73 |
-
{
|
74 |
-
"name": "give_response",
|
75 |
-
"description": "Utilisez cette fonction pour donner la réponse et si la réponse à la question a été trouvée ou non dans le texte.",
|
76 |
-
"parameters": {
|
77 |
-
"type": "object",
|
78 |
-
"properties": {
|
79 |
-
"answer_found": {
|
80 |
-
"type": "boolean",
|
81 |
-
"description": "Définissez ceci sur true uniquement si le texte fourni inclut une réponse à la question",
|
82 |
-
},
|
83 |
-
"response": {
|
84 |
-
"type": "string",
|
85 |
-
"description": "La réponse complète à la question, si l'information était pertinente",
|
86 |
-
},
|
87 |
-
},
|
88 |
-
},
|
89 |
-
"required": ["answer_found"],
|
90 |
-
}
|
91 |
-
],
|
92 |
-
)
|
93 |
-
|
94 |
-
try:
|
95 |
-
function_call = response["choices"][0]["message"]["function_call"]
|
96 |
-
arguments = function_call["arguments"].lower()
|
97 |
-
result = json.loads(arguments)
|
98 |
-
return result
|
99 |
-
except KeyError:
|
100 |
-
return {"answer_found": False, "response": ""}
|
|
|
1 |
import openai
|
|
|
2 |
|
3 |
model = "gpt-3.5-turbo"
|
4 |
|
|
|
5 |
def get_keywords(question):
|
6 |
+
prompt = f"Extract 10 keywords from the following question, including synonyms. Use only lowercase letters:\n\n{question}"
|
7 |
+
response = openai.Completion.create(model=model, prompt=prompt, max_tokens=60)
|
8 |
+
keywords = response.choices[0].text.strip().split(', ')
|
9 |
+
return keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def answer_question(chunk, question):
|
12 |
+
prompt = f"Based on the following information, what is the answer to this question?\n\nText:\n{chunk}\n\nQuestion:\n{question}"
|
13 |
+
response = openai.Completion.create(model=model, prompt=prompt, max_tokens=150)
|
14 |
+
answer = response.choices[0].text.strip()
|
15 |
+
return answer
|
16 |
+
|
17 |
+
# Example usage
|
18 |
+
question = "What are the key aspects of climate change?"
|
19 |
+
keywords = get_keywords(question)
|
20 |
+
print("Keywords:", keywords)
|
21 |
+
|
22 |
+
chunk = "Climate change is a long-term change in the average weather patterns..."
|
23 |
+
answer = answer_question(chunk, question)
|
24 |
+
print("Answer:", answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|