Spaces:
Running
Running
Zekun Wu
commited on
Commit
•
117a821
1
Parent(s):
453640a
update
Browse files
app.py
CHANGED
@@ -1,4 +1,72 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import http.client
|
3 |
import streamlit as st
|
4 |
+
from openai import AzureOpenAI
|
5 |
|
6 |
+
class ContentFormatter:
|
7 |
+
@staticmethod
|
8 |
+
def chat_completions(text, settings_params):
|
9 |
+
message = [
|
10 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
11 |
+
{"role": "user", "content": text}
|
12 |
+
]
|
13 |
+
data = {"messages": message, **settings_params}
|
14 |
+
return json.dumps(data)
|
15 |
+
|
16 |
+
class AzureAgent:
|
17 |
+
def __init__(self, api_key, azure_uri):
|
18 |
+
self.azure_uri = azure_uri
|
19 |
+
self.headers = {
|
20 |
+
'Authorization': f"Bearer {api_key}",
|
21 |
+
'Content-Type': 'application/json'
|
22 |
+
}
|
23 |
+
self.chat_formatter = ContentFormatter
|
24 |
+
|
25 |
+
def invoke(self, text, **kwargs):
|
26 |
+
body = self.chat_formatter.chat_completions(text, {**kwargs})
|
27 |
+
conn = http.client.HTTPSConnection(self.azure_uri)
|
28 |
+
conn.request("POST", '/v1/chat/completions', body=body, headers=self.headers)
|
29 |
+
response = conn.getresponse()
|
30 |
+
data = response.read()
|
31 |
+
conn.close()
|
32 |
+
decoded_data = data.decode("utf-8")
|
33 |
+
parsed_data = json.loads(decoded_data)
|
34 |
+
content = parsed_data["choices"][0]["message"]["content"]
|
35 |
+
return content
|
36 |
+
|
37 |
+
class GPTAgent:
|
38 |
+
def __init__(self, api_key, azure_endpoint):
|
39 |
+
self.client = AzureOpenAI(
|
40 |
+
api_key=api_key,
|
41 |
+
api_version='2022-11-01', # Defaulting to a specific API version
|
42 |
+
azure_endpoint=azure_endpoint
|
43 |
+
)
|
44 |
+
self.deployment_name = 'your-model-deployment-name' # Update this based on actual deployment name
|
45 |
+
|
46 |
+
def invoke(self, text, **kwargs):
|
47 |
+
response = self.client.chat.completions.create(
|
48 |
+
model=self.deployment_name,
|
49 |
+
messages=[
|
50 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
51 |
+
{"role": "user", "content": text}
|
52 |
+
],
|
53 |
+
**kwargs
|
54 |
+
)
|
55 |
+
return response.choices[0].message.content
|
56 |
+
|
57 |
+
# Streamlit app interface
|
58 |
+
st.title('Model Invocation with User Credentials')
|
59 |
+
|
60 |
+
model_type = st.radio("Select the type of agent", ('AzureAgent', 'GPTAgent'))
|
61 |
+
api_key = st.text_input("API Key", type="password")
|
62 |
+
endpoint_url = st.text_input("Endpoint URL")
|
63 |
+
|
64 |
+
input_text = st.text_area('Enter your text')
|
65 |
+
if st.button('Invoke Model'):
|
66 |
+
if model_type == 'AzureAgent':
|
67 |
+
agent = AzureAgent(api_key, endpoint_url)
|
68 |
+
else:
|
69 |
+
agent = GPTAgent(api_key, endpoint_url)
|
70 |
+
|
71 |
+
output = agent.invoke(input_text, temperature=0, max_tokens=5)
|
72 |
+
st.write('Response:', output)
|