-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxstreamsearchk.py
226 lines (192 loc) · 6.84 KB
/
xstreamsearchk.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#! -*- coding: utf-8 -*-
import requests
import json
from settings import get_llm_url,get_llm_model,get_api_type,API_OPENAI,MAX_TOKENS,TEMPERATURE
from streamprocess import StreamProcess
def get_prompt(sourcetext, mode=None):
if mode=='chat':
systemPrompt = ""
else:
systemPrompt = f"Only use the following information to answer the question and provide the images if possible. Do not use anything else, if there is any http link, return it as this format <http://>: {sourcetext}"
if mode == 'code':
systemPrompt += "\n\n. You are in Omniverse scene. suppose the up axis is Y. don't define any function and only generate the code. code should be included within '''. and only generate one answer. \n Question: write python code to "
print(f'systemPrompt:{systemPrompt}')
print(f'systemPrompt len:{len(systemPrompt)}')
return systemPrompt
async def streamquery(question, callback, sourcetext, mode=None, context = []):
print("streamquery:%s"%question)
code_mode = False
url = get_llm_url()
if get_api_type() == API_OPENAI:
payload = {
"model": get_llm_model(),
"prompt":get_prompt(sourcetext, mode) + question,
"stream": True,
"max_tokens": MAX_TOKENS,
"temperature": TEMPERATURE
}
else:
payload = {
"model": get_llm_model(),#"mistral-openorca",
"prompt": question,
"system": sourcetext,
"stream": True,
"context": context
}
# Convert the payload to a JSON string
payload_json = json.dumps(payload)
# Set the headers to specify JSON content
headers = {
"Content-Type": "application/json"
}
proc = StreamProcess()
# Send the POST request
r = requests.post(url, data=payload_json, headers=headers,
stream=True)
r.raise_for_status()
if code_mode:
await callback("", text_type='code_begin')
for line in r.iter_lines():
print(line)
if get_api_type() == API_OPENAI:
line = line.decode("utf8")
#print(line)
if line.startswith("data:"):
line = line[5:]
line = line.strip()
if line.startswith("[DONE"):
break
if not line.startswith("{"):
continue
body = json.loads(line.strip())
response_part = body.get("choices")[0]['text']
else:
body = json.loads(line)
response_part = body.get('response', '')
if code_mode:
await callback(response_part, text_type='code_continue')
continue
await proc.process_chat(response_part, callback=callback)
if 'error' in body:
raise Exception(body['error'])
if body.get('done', False):
break
if code_mode:
await callback(proc.code, text_type='code_end')
else:
await callback(proc.text)
def query(question, callback, sourcetext, mode=None, context = []):
print("query:%s"%question)
code_mode = False
url = get_llm_url()
if get_api_type() == API_OPENAI:
payload = {
"model": get_llm_model(),
"prompt":get_prompt(sourcetext, mode) + question,
"stream": True,
"max_tokens": MAX_TOKENS,
"temperature": TEMPERATURE
}
else:
payload = {
"model": get_llm_model(),#"mistral-openorca",
"prompt": question,
"system": sourcetext,
"stream": True,
"context": context
}
# Convert the payload to a JSON string
payload_json = json.dumps(payload)
# Set the headers to specify JSON content
headers = {
"Content-Type": "application/json"
}
# Send the POST request
r = requests.post(url, data=payload_json, headers=headers,
stream=True)
r.raise_for_status()
if code_mode:
callback("", text_type='code_begin')
for line in r.iter_lines():
if get_api_type() == API_OPENAI:
line = line.decode("utf8")
#print(line)
if line.startswith("data:"):
line = line[5:]
line = line.strip()
if line.startswith("[DONE"):
break
if not line.startswith("{"):
continue
body = json.loads(line.strip())
response_part = body.get("choices")[0]['text']
else:
body = json.loads(line)
response_part = body.get('response', '')
if code_mode:
callback(response_part, text_type='code_continue')
continue
callback(response_part, text_type='char')
if 'error' in body:
raise Exception(body['error'])
if body.get('done', False):
break
def chat(question, callback, sourcetext, mode=None):
print("chat:%s"%question)
code_mode = False
url = get_llm_url()
if get_api_type() == API_OPENAI:
payload = {
"model": get_llm_model(),
"prompt":get_prompt(sourcetext, mode) + question,
"stream": True,
"max_self.tokens": MAX_TOKENS,
"temperature": TEMPERATURE
}
else:
payload = {
"model": get_llm_model(),
"messages": [
{
"role": "user",
"content": question
}
],
"stream": True
}
# Convert the payload to a JSON string
payload_json = json.dumps(payload)
# Set the headers to specify JSON content
headers = {
"Content-Type": "application/json"
}
# Send the POST request
r = requests.post(url, data=payload_json, headers=headers,
stream=True)
r.raise_for_status()
if code_mode:
callback("", text_type='code_begin')
for line in r.iter_lines():
if get_api_type() == API_OPENAI:
line = line.decode("utf8")
#print(line)
if line.startswith("data:"):
line = line[5:]
line = line.strip()
if line.startswith("[DONE"):
break
if not line.startswith("{"):
continue
body = json.loads(line.strip())
response_part = body.get("choices")[0]['text']
else:
body = json.loads(line)
response_part = body.get('message', {}).get("content","")
if code_mode:
callback(response_part, text_type='code_continue')
continue
callback(response_part, text_type='char')
if 'error' in body:
raise Exception(body['error'])
if body.get('done', False):
break