forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot_workflow.py
More file actions
72 lines (58 loc) · 2.48 KB
/
chatbot_workflow.py
File metadata and controls
72 lines (58 loc) · 2.48 KB
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
# http://dify.wangyulong.io:3000/app/9f825857-942c-410f-941f-cf10ae7f9b5b/develop
import re
import requests
def gpt_completion_helper(prompt, conversation_id=''):
url = 'http://dify.wangyulong.io:5001/v1/chat-messages'
api_key = 'app-HJYTmx932Mk1ZC348g7VoHkG' #'app-5OocbNmeFzKruQAp1euOp99a'
headers = {'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json'}
request = {
'inputs': {},
'query': f'{prompt}',
'response_mode': 'blocking',
'conversation_id': conversation_id,
'user': 'yuantx',
}
response = requests.post(url, headers=headers, json=request)
s = response.content.decode('utf-8')
if conversation_id == '':
conversation_id = extract_conversation_id(s)
content = extract_resp(s)
return content, conversation_id
def extract_resp(s):
pattern = r'"answer": "(.*?)"'
matches = re.findall(pattern, s)
content = ''
for match in matches:
content += match
return content
def extract_conversation_id(s):
pattern_conversation = r'"conversation_id": "(.*?)"'
matches_conversation = re.findall(pattern_conversation, s)
conversation_id = matches_conversation[0]
return conversation_id
"""
def gpt_completion(model, messages):
conversation_id = ''
query = messages[-1]
for query in messages: # 倒序调用
if query['role'] == 'user':
conversation_id = gpt_completion_helper(query['content'], conversation_id)
elif query['role'] == 'assistant':
return
prompt = "hello, print '20240630' please."
content, conversation_id = gpt_completion_helper(prompt)
print(content)
prompt = "hello, print '15:22', and attach the last sentence i ask you to print."
content, conversation_id = gpt_completion_helper(prompt, conversation_id)
print(content)
"""
"""
{"event": "message",
"task_id": "97bcf1a2-6795-4c2c-aeca-5ef993096d59",
"id": "0599ef72-beae-428a-b700-c1b7be2086f0",
"message_id": "0599ef72-beae-428a-b700-c1b7be2086f0",
"conversation_id": "da541854-2899-40a0-9bb9-fda3e25d42e7",
"mode": "advanced-chat",
"answer": "Your configuration is correct",
"metadata": {"usage": {"prompt_tokens": 18, "prompt_unit_price": "0.03", "prompt_price_unit": "0.001", "prompt_price": "0.0005400", "completion_tokens": 11, "completion_unit_price": "0.06", "completion_price_unit": "0.001", "completion_price": "0.0006600", "total_tokens": 29, "total_price": "0.0012000", "currency": "USD", "latency": 0.916656875051558}}, "created_at": 1717573770}
"""