update
This commit is contained in:
parent
9403b2b3e7
commit
e8aeb002fd
10
README.md
10
README.md
@ -149,3 +149,13 @@ python -m streamlit run ./Qwen_Turbo.py --theme.base dark --server.port 8501
|
||||
```
|
||||
python -m streamlit run ./星火大模型.py --theme.base dark --server.port 8501
|
||||
```
|
||||
|
||||
#### 4. 百度 - 零一万物的开源模型 Yi-34B-Chat
|
||||
|
||||
百度千帆大模型平台的 API key 获取(大部分收费,有个别限时免费):https://console.bce.baidu.com/qianfan/overview
|
||||
|
||||
运行命令:
|
||||
|
||||
```
|
||||
python -m streamlit run ./Yi-34B-Chat.py --theme.base dark --server.port 8501
|
||||
```
|
86
模型API - 百度 - 零一万物的开源模型 Yi-34B-Chat/Yi-34B-Chat.py
Normal file
86
模型API - 百度 - 零一万物的开源模型 Yi-34B-Chat/Yi-34B-Chat.py
Normal file
@ -0,0 +1,86 @@
|
||||
import streamlit as st
|
||||
st.set_page_config(
|
||||
page_title="Chat",
|
||||
layout='wide'
|
||||
)
|
||||
|
||||
import requests
|
||||
import json
|
||||
|
||||
def get_access_token():
|
||||
"""
|
||||
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
|
||||
"""
|
||||
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
|
||||
|
||||
payload = json.dumps("")
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
response = requests.request("POST", url, headers=headers, data=payload)
|
||||
return response.json().get("access_token")
|
||||
|
||||
|
||||
with st.sidebar:
|
||||
with st.expander('参数', expanded=True):
|
||||
top_p = st.slider('top_p', 0.01, 1.0, step=0.01, value=0.8, key='top_p_session')
|
||||
temperature = st.slider('temperature', 0.51, 1.0, step=0.01, value=0.85, key='temperature_session')
|
||||
def reset_parameter():
|
||||
st.session_state['top_p_session'] = 0.8
|
||||
st.session_state['temperature_session'] = 0.85
|
||||
reset_parameter_button = st.button('重置', on_click=reset_parameter)
|
||||
|
||||
prompt = st.chat_input("在这里输入您的命令")
|
||||
|
||||
def clear_all():
|
||||
st.session_state.messages = []
|
||||
st.session_state.ai_response = []
|
||||
|
||||
if 'messages' not in st.session_state:
|
||||
st.session_state.messages = []
|
||||
if 'ai_response' not in st.session_state:
|
||||
st.session_state.ai_response = []
|
||||
|
||||
for ai_response in st.session_state.ai_response:
|
||||
with st.chat_message(ai_response["role"], avatar=ai_response.get("avatar")):
|
||||
st.markdown(ai_response["content"])
|
||||
|
||||
prompt_placeholder = st.chat_message("user", avatar='user')
|
||||
with st.chat_message("robot", avatar="assistant"):
|
||||
message_placeholder_yi_34b = st.empty()
|
||||
|
||||
def response_of_yi_34b(prompt):
|
||||
st.session_state.messages.append({'role': "user", 'content': prompt})
|
||||
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=" + get_access_token()
|
||||
payload = json.dumps({
|
||||
"messages": st.session_state.messages,
|
||||
"top_p": top_p,
|
||||
"temperature": temperature,
|
||||
"stream": True
|
||||
})
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
response = requests.request("POST", url, headers=headers, data=payload, stream=True)
|
||||
full_content = ''
|
||||
for line in response.iter_lines():
|
||||
try:
|
||||
dict_data = json.loads(line.decode("UTF-8")[5:])
|
||||
full_content += dict_data['result']
|
||||
message_placeholder_yi_34b.markdown(full_content)
|
||||
except:
|
||||
pass
|
||||
if stop_button:
|
||||
break
|
||||
st.session_state.messages.append({'role': "assistant",
|
||||
'content': full_content})
|
||||
st.session_state.ai_response.append({"role": "robot", "content": full_content, "avatar": "assistant"})
|
||||
return full_content
|
||||
|
||||
if prompt:
|
||||
prompt_placeholder.markdown(prompt)
|
||||
st.session_state.ai_response.append({"role": "user", "content": prompt, "avatar": 'user'})
|
||||
stop = st.empty()
|
||||
stop_button = stop.button('停止', key='break_response')
|
||||
response_of_yi_34b(prompt)
|
||||
stop.empty()
|
||||
button_clear = st.button("清空", on_click=clear_all, key='clear')
|
Loading…
x
Reference in New Issue
Block a user