-
Notifications
You must be signed in to change notification settings - Fork 0
/
prova.py
105 lines (83 loc) · 2.92 KB
/
prova.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import requests
import openai
# Sostituisci '[api-key]' con la tua chiave API
api_key = ''
# URL del servizio GraphQL
url = f'https://api.thegraph.com/subgraphs/name/ensdomains/ens'
with open("backend/app/graphql/ens.root.object", "r") as f:
q_roots = f.read()
with open("backend/app/graphql/ens.graphql", "r") as f:
txt = f.read()
# print(txt)
# Funzione per generare la query GraphQL utilizzando GPT-3.5
def generate_graphql_query(p):
prompt=f"""
Given the following graphql schema:
```
{txt}
```
Translate the following into a syntactically valid graphql query.
Try to not invent new fields, but use the ones already defined in the schema.
Prefer less precise results over probably failing queries.
Give me only the query source.
```
${p}
```
"""
print(prompt)
response = openai.Completion.create(
model="gpt-3.5-turbo-instruct",
# engine="davinci-002",
prompt=prompt,
max_tokens=200
)
return response.choices[0].text.strip().replace("`", "").strip()
retry = True
while retry:
# Prompt per generare la query GraphQL
prompt = "Give me the first 3 domains"
query = generate_graphql_query(prompt)
print("------------------")
print(query)
print("------------------")
# Parametri della richiesta GraphQL
variables = {}
# Creazione della richiesta POST
response = requests.post(url, json={'query': query, 'variables': variables})
# Verifica della risposta
if not response.status_code == 200:
data = response.json()
print(f'Errore nella richiesta GraphQL: {response.status_code}')
print(response.text)
retry = True # Set retry to True to retry the request
else:
graphql_response = response.json()
if "errors" in graphql_response:
print("GraphQL response contains errors. Retrying...")
retry = True
else:
retry = False # Set retry to False to stop retrying
# Verifica della risposta
if not response.status_code == 200:
data = response.json()
print(f'Errore nella richiesta GraphQL: {response.status_code}')
print(response.text)
exit(1)
promptai = f"""
You are CarbonarAI, a friendly and helpful AI assistant by developed at EthRome2023 that provides help with interpreting GraphQL responses.
You give thorough answers. Use the following pieces of context to help answer the users question. If its not relevant to the question, provide friendly responses.
If you cannot answer the question or find relevant meaning in the context, tell the user to try re-phrasing the question. Use the settings below to configure this prompt and your answers.
<User Query>
{prompt}
<response>
```
{response.text}
```
"""
print(promptai)
f = openai.Completion.create(
model="gpt-3.5-turbo-instruct",
prompt=promptai,
max_tokens=200
)
print(f.choices[0].text)