1
+ import getpass
2
+ import os
3
+
4
+ from typing import *
5
+ import json
6
+ import sys
7
+ import time
8
+ import subprocess
9
+ import traceback
10
+ from tempfile import NamedTemporaryFile
11
+ # from google.colab.patches import cv2_imshow
12
+
13
+ import requests
14
+ import openai
15
+ # from dotenv import load_dotenv
16
+ import xml .etree .ElementTree as ET
17
+ from datetime import datetime
18
+ import re
19
+ from date_parser import extract_day_month
20
+
21
+
22
+ # if not os.environ.get('OPENAI_API_KEY'):
23
+ # os.environ['OPENAI_API_KEY'] = getpass.getpass("Enter the OpenAI API Key(which starts with sk-): ")
24
+
25
+ # load_dotenv() # This loads the variables from .env
26
+
27
+ api_key = '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'
28
+ assistant_id = 'asst_FECUwGnkMA50kVsyeGcbdxJI'
29
+
30
+ client = openai .Client (api_key = api_key )
31
+
32
+ def execute_python_code (s : str ) -> str :
33
+ with NamedTemporaryFile (suffix = '.py' , delete = False ) as temp_file :
34
+ temp_file_name = temp_file .name
35
+ temp_file .write (s .encode ('utf-8' ))
36
+ temp_file .flush ()
37
+ try :
38
+ result = subprocess .run (
39
+ ['python' , temp_file_name ],
40
+ capture_output = True ,
41
+ text = True ,
42
+ check = True
43
+ )
44
+ return result .stdout
45
+ except subprocess .CalledProcessError as e :
46
+ return e .stderr
47
+ finally :
48
+ import os
49
+ os .remove (temp_file_name )
50
+
51
+ def get_lottery_result (lottery_name , date ):
52
+ # https://xskt.com.vn/rss-feed/mien-bac-xsmb.rss
53
+ # return "Toi khong biet!"
54
+ # Chuyển đổi ngày từ chuỗi sang đối tượng datetime
55
+ converted_date = extract_day_month (date )
56
+ if converted_date :
57
+ print (f"Converted date: { converted_date } " )
58
+ else :
59
+ print ("Invalid date format" )
60
+
61
+ date = datetime .strptime (converted_date , '%d/%m' )
62
+ rss_url = 'https://xskt.com.vn/rss-feed/mien-bac-xsmb.rss'
63
+
64
+ # Tải dữ liệu RSS từ URL
65
+ response = requests .get (rss_url )
66
+ root = ET .fromstring (response .content )
67
+
68
+ # Tìm kiếm kết quả trong các mục
69
+ for item in root .findall ('.//item' ):
70
+ title = item .find ('title' ).text
71
+ # Kiểm tra xem tiêu đề có chứa chuỗi ngày/tháng hay không
72
+ if converted_date in title :
73
+ return item .find ('description' ).text
74
+
75
+ return "Không tìm thấy kết quả xổ số cho ngày này."
76
+
77
+ def run_assistant (client , assistant_id , thread_id ):
78
+ # Create a new run for the given thread and assistant
79
+ run = client .beta .threads .runs .create (
80
+ thread_id = thread_id ,
81
+ assistant_id = assistant_id
82
+ )
83
+
84
+ # Loop until the run status is either "completed" or "requires_action"
85
+ while run .status == "in_progress" or run .status == "queued" :
86
+ time .sleep (3 )
87
+ run = client .beta .threads .runs .retrieve (
88
+ thread_id = thread_id ,
89
+ run_id = run .id
90
+ )
91
+
92
+ # At this point, the status is either "completed" or "requires_action"
93
+ if run .status == "completed" :
94
+ return client .beta .threads .messages .list (
95
+ thread_id = thread_id
96
+ )
97
+ if run .status == "requires_action" :
98
+ tool_call = run .required_action .submit_tool_outputs .tool_calls [0 ]
99
+ tool_outputs = []
100
+ if tool_call .function .name == "execute_python_code" :
101
+ generated_python_code = json .loads (tool_call .function .arguments )['code' ]
102
+ result = execute_python_code (generated_python_code )
103
+ tool_outputs .append (
104
+ {
105
+ "tool_call_id" : tool_call .id ,
106
+ "output" : result ,
107
+ },
108
+ )
109
+ # Mike add
110
+ elif tool_call .function .name == "get_lottery_result" :
111
+ lottery_name = json .loads (tool_call .function .arguments )['lottery_name' ]
112
+ draw_date = json .loads (tool_call .function .arguments )['draw_date' ]
113
+ result = get_lottery_result (lottery_name , draw_date )
114
+ tool_outputs .append (
115
+ {
116
+ "tool_call_id" : tool_call .id ,
117
+ "output" : result ,
118
+ },
119
+ )
120
+
121
+ if tool_outputs :
122
+ run = client .beta .threads .runs .submit_tool_outputs (
123
+ thread_id = thread_id ,
124
+ run_id = run .id ,
125
+ tool_outputs = tool_outputs
126
+ )
127
+
128
+ if __name__ == "__main__" :
129
+ script = input ("python code to execute: \n " )
130
+
131
+
132
+ # Create a new thread
133
+ thread = client .beta .threads .create ()
134
+ thread_id = thread .id
135
+ # Create a new thread message with the provided task
136
+ thread_message = client .beta .threads .messages .create (
137
+ thread .id ,
138
+ role = "user" ,
139
+ content = script ,
140
+ )
141
+
142
+ # assistant_id, thread_id = setup_assistant(client, script)
143
+ print (f"Debugging: Useful for checking the generated agent in the playground. https://platform.openai.com/playground?mode=assistant&assistant={ assistant_id } " )
144
+ print (f"Debugging: Useful for checking logs. https://platform.openai.com/playground?thread={ thread_id } " )
145
+
146
+ messages = run_assistant (client , assistant_id , thread_id )
147
+
148
+ # message_dict = json.loads(messages)
149
+ print (messages )
0 commit comments