forked from chagong/MyFirstCopilotPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
144 lines (122 loc) · 4.45 KB
/
app.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
import json
import os
import openai
import requests
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
from datetime import datetime
app = Flask(__name__)
CORS(app)
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.azure_endpoint = os.getenv("OPENAI_API_BASE")
openai.api_type = "azure"
model_name = os.getenv("OPENAI_MODEL_NAME")
functions = [
{
"name": "get_bing_search_results",
"description": "Get the search results from Bing search engine.",
"parameters": {
"type": "object",
"properties": {
"term": {
"type": "string",
"description": "The search term."
}
},
"required": ["term"],
}
}
]
prompts = """
- Do your best to answer user's questions.
- Find out what user really wants to know and give exhaustive answer.
- Never say to user you don't know the answer, instead try to get answers from Bing search.
- Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.
- You should always conclude the answer from Bing earch results.
- Conclude the answer from snippet text frist in Bing search results.
"""
def get_bing_search_results(search_request):
search_term = search_request.get("term")
subscription_key = os.getenv("BING_ACCESS_KEY")
assert subscription_key
search_url = 'https://api.bing.microsoft.com/v7.0/search'
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
params = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
values = data.get('webPages', {}).get('value', [])
return values
def generate(data_stream):
try:
for chunk in data_stream:
yield f"data: {chunk.json()}\n\n"
finally:
data_stream.close()
@app.route('/oauth/callback')
def oauth_callback():
return jsonify({'ok': 'true'}), 200
@app.route('/handle-turn', methods=['POST'])
def handle_turn():
# Check the content type
if request.headers['Content-Type'] != 'application/json':
return jsonify({'error': 'Invalid Content-Type'}), 400
try:
# Check the request body
data = request.json
messages = data.get('messages')
if messages is None:
return jsonify({'error': 'Missing prompts in the request body'}), 400
messages = list(
map(lambda x: {'role': x['role'], 'content': x['content']}, messages))
messages.insert(
0,
{
"role": "system",
"content": prompts
}
)
chat_completion = openai.chat.completions.create(
model=model_name,
messages=messages,
functions=functions,
)
if chat_completion.choices[0].finish_reason == 'function_call':
function_call = chat_completion.choices[0].message.function_call
if function_call.name == 'get_bing_search_results':
response = get_bing_search_results(
json.loads(function_call.arguments))
print(response)
messages.append(
{
"role": "function",
"name": "get_bing_search_results",
"content": json.dumps(response)
}
)
data_stream = openai.chat.completions.create(
model=model_name,
messages=messages,
# functions=functions,
stream=True,
)
return Response(generate(data_stream), mimetype='text/event-stream')
else:
chunk_dict = {
'id': "chunk",
'object': "chat.completion.chunk",
'created': int(datetime.now().timestamp()),
'model': model_name,
'choices': [
{
'index': 0,
'delta': {
'content': chat_completion.choices[0].message.content
},
'finish_reason': chat_completion.choices[0].finish_reason
}
]
}
return f"data: {json.dumps(chunk_dict)}\n\n"
except Exception as e:
return jsonify({'error': f'{e}'}), 400