-
Notifications
You must be signed in to change notification settings - Fork 253
/
vision_crawl.py
111 lines (95 loc) · 3.25 KB
/
vision_crawl.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
from openai import OpenAI
import subprocess
import base64
import json
import os
model = OpenAI()
model.timeout = 10
def image_b64(image):
with open(image, "rb") as f:
return base64.b64encode(f.read()).decode()
prompt = input("You: ")
messages = [
{
"role": "system",
"content": "You are a web crawler. Your job is to give the user a URL to go to in order to find the answer to the question. Go to a direct URL that will likely have the answer to the user's question. Respond in the following JSON fromat: {\"url\": \"<put url here>\"}",
},
{
"role": "user",
"content": prompt,
}
]
while True:
while True:
response = model.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
max_tokens=1024,
response_format={"type": "json_object"},
seed=2232,
)
message = response.choices[0].message
message_json = json.loads(message.content)
url = message_json["url"]
messages.append({
"role": "assistant",
"content": message.content,
})
print(f"Crawling {url}")
if os.path.exists("screenshot.jpg"):
os.remove("screenshot.jpg")
result = subprocess.run(
["node", "screenshot.js", url],
capture_output=True,
text=True
)
exitcode = result.returncode
output = result.stdout
if not os.path.exists("screenshot.jpg"):
print("ERROR: Trying different URL")
messages.append({
"role": "user",
"content": "I was unable to crawl that site. Please pick a different one."
})
else:
break
b64_image = image_b64("screenshot.jpg")
response = model.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "system",
"content": "Your job is to answer the user's question based on the given screenshot of a website. Answer the user as an assistant, but don't tell that the information is from a screenshot or an image. Pretend it is information that you know. If you can't answer the question, simply respond with the code `ANSWER_NOT_FOUND` and nothing else.",
}
] + messages[1:] + [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{b64_image}",
},
{
"type": "text",
"text": prompt,
}
]
}
],
max_tokens=1024,
)
message = response.choices[0].message
message_text = message.content
if "ANSWER_NOT_FOUND" in message_text:
print("ERROR: Answer not found")
messages.append({
"role": "user",
"content": "I was unable to find the answer on that website. Please pick another one"
})
else:
print(f"GPT: {message_text}")
prompt = input("\nYou: ")
messages.append({
"role": "user",
"content": prompt,
})