-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (111 loc) · 4.41 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
import base64
import requests
import base64
from PIL import ImageGrab
from io import BytesIO
# OpenAI API Key
api_key = ""
class Conversation:
def __init__(self, api_key, model="gpt-4-vision-preview", max_tokens=500):
"""
Initializes the API client with the provided API key, model name, and maximum number of tokens.
Parameters:
- api_key (str): The API key used to authenticate the client.
- model (str, optional): The name of the model to use. Defaults to "gpt-4-vision-preview".
- max_tokens (int, optional): The maximum number of tokens allowed in the generated output. Defaults to 500.
Returns:
None
"""
self.api_key = api_key
self.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
self.model = model
self.max_tokens = max_tokens
self.system_instruction = "You are a helpful crypto trader assistant. You will be given a chart and asked to perform technical analysis. On image you will have chart with 50 and 200 MA, under chart you will have volume and bellow it RSI. Try to recognize patterns which can tell where price will go. Keep it short, concise and to the point. Short term should I short or long futures or not enter at the moment?"
self.initial_payload = {
"model": self.model,
"messages": [
{
"role": "system",
"content": self.system_instruction,
},
],
"max_tokens": self.max_tokens,
}
self.payload = self.initial_payload
def get_encoded_image_from_clipboard(self):
# Grab the image from the clipboard
image = ImageGrab.grabclipboard()
# Check if there is an image in the clipboard
if isinstance(image, ImageGrab.Image.Image):
# Create a BytesIO object to hold the image data
buffered = BytesIO()
# Save the image to the BytesIO object in PNG format
image.save(buffered, format="PNG")
# Retrieve the binary image data from the BytesIO object
img_byte = buffered.getvalue()
# Convert the binary data to a base64-encoded string
img_base64 = base64.b64encode(img_byte)
# Convert the base64 bytes to a string
img_base64_str = img_base64.decode("utf-8")
# Output the Base64 string
return img_base64_str
else:
raise Exception("No image data is found in the clipboard.")
def submit_chart(
self, instruction="Perform Technical analysis based off of this chart."
):
self.payload["messages"].append(
{
"role": "user",
"content": [
{
"type": "text",
"text": instruction,
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{self.get_encoded_image_from_clipboard()}",
"detail": "high",
},
},
],
}
)
def handler(self):
if len(self.payload["messages"]) == 1:
self.submit_chart()
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=self.headers,
json=self.payload,
)
response.raise_for_status()
response_json = response.json()
if response_json.get("choices"):
msg = response_json["choices"][0]["message"]["content"]
self.payload["messages"].append(
{
"role": "assistant",
"content": msg,
}
)
print(msg + "\n")
new_msg = input("Enter your response: ")
if new_msg == "chart":
new_msg = input("Chart instruction: ")
self.submit_chart(new_msg)
else:
self.payload["messages"].append(
{
"role": "user",
"content": new_msg,
}
)
print("\n")
self.handler()
conversation = Conversation(api_key)
conversation.handler()