-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoc_code.py
121 lines (93 loc) · 3.75 KB
/
doc_code.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
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import sys
# attempt to prevent running locally
def is_running_in_docker():
return os.path.exists('/.dockerenv')
if is_running_in_docker():
print("We appear to be running in Docker...")
else:
print("You are not allowed to run this locally. See the `DISCLAIMER.md` file.")
sys.exit()
import weaviate
import config
import pprint
import json
import time
import random
import traceback
from lib.util import random_string, get_username
from lib.database import weaviate_schema, weaviate_query, weaviate_update, weaviate_object, featurebase_query
from lib.ai import ai
from coolname import generate_slug
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import TerminalFormatter
# user
username = get_username()
# create Weaviate schema
weaviate_schema = weaviate_schema("Code")
# get files to process
filename = "python.pdf"
def go_weaviate(question, filename="", move_tos=[], num_results=20):
start_time = time.time()
# lookup matches from weaviate's QandAs
weaviate_results = weaviate_query([question], "PDFs", ["filename", "page_id", "fragment"], move_tos=move_tos, filename=filename, num_results=num_results)
end_time = time.time()
elapsed_time = end_time - start_time
print("system> Queried Weaviate for questions in:", elapsed_time, "seconds")
print("system> %s results found." % len(weaviate_results))
# print(weaviate_results[0])
# print(weaviate_results[0].get('fragment'))
return weaviate_results
# build history and session
from prompt_toolkit import PromptSession
from prompt_toolkit.history import FileHistory
history = FileHistory(".DoctorGPT")
session = PromptSession(history=history)
while True:
# get a query from the user
# TODO
# 1. write code fragments into weaviate and featurebase for prompt assembly (to help write more complicated code)
# 2. call the AI model for extracting keyterms and insert into featurebase, along with the code
# 3. describe what the code does so we can better search for it later using keyterms
try:
question = session.prompt("%s[%s]> " % (username, "python3"))
except KeyboardInterrupt:
print("system>", random.choice(["Bye!", "Later!", "Nice working with you."]))
sys.exit()
# build the document for the AI calls
move_tos = ['python3']
document = {"question": question, "text": "print('hello world')", "keyterms": move_tos}
# call the AI
print("system> Calling GPTChat for code...please wait.")
# we'll try up to X times to get GPTChat to write the code, passing failures in for correction
for x in range(4):
try:
document = ai("ask_gptcode", document)
answer = document['answer']
if answer.find('```python') != -1:
start_index = answer.find('```python') + len('```python')
end_index = answer.find('```', start_index)
code = answer[start_index:end_index]
else:
code = answer
print("system> Showing code...")
print(highlight(code, PythonLexer(), TerminalFormatter()))
print("system> Running code...")
exec(code)
# get keyterms (and a related question)
document_terms = {"code": code, "prompt": question}
document_terms = ai("gpt_codeterms", document_terms)
explanation = document_terms.get('explanation')
if not explanation.endswith('.'):
explanation += "."
print("system>", explanation)
break
except Exception as e:
traceback_str = traceback.format_exc()
error_msg = str(e)
print("system>", error_msg)
print("system> Providing traceback to ChatGPT for correction...")
document['text'] = code + "\n" + traceback_str + "\n" + error_msg + "\nRemember, we are using '''start_index = answer.find('```python') + len('```python')''' to detect code to run. Please follow that format.\n"
else:
print("system> Failed to compile a valid solution. Please provide one in a document and retry.")