Skip to content

Commit

Permalink
Merge pull request Ikaros-521#830 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
千帆AppBuilder更新新接口的对接
  • Loading branch information
Ikaros-521 authored May 16, 2024
2 parents 35afa05 + 8652fb4 commit f769057
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
"model": "ERNIEBot",
"api_key": "",
"secret_key": "",
"app_id": "7823c970-c672-48a3-8162-ef24e3b7dc5b",
"app_token": "",
"top_p": 0.8,
"temperature": 0.9,
Expand Down
1 change: 1 addition & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
"model": "ERNIEBot",
"api_key": "",
"secret_key": "",
"app_id": "7823c970-c672-48a3-8162-ef24e3b7dc5b",
"app_token": "",
"top_p": 0.8,
"temperature": 0.9,
Expand Down
68 changes: 59 additions & 9 deletions utils/gpt_model/my_wenxinworkshop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, logging, traceback
import json, logging, traceback, requests
from wenxinworkshop import LLMAPI, AppBuilderAPI, EmbeddingAPI, PromptTemplateAPI
from wenxinworkshop import Message, Messages, Texts

Expand All @@ -19,6 +19,8 @@ def __init__(self, data):

self.my_bot = None

self.conversation_id = None

logging.debug(self.config_data)

try:
Expand Down Expand Up @@ -51,14 +53,37 @@ def __init__(self, data):
url=model_url_map[selected_model]
)
elif self.config_data['type'] == "AppBuilder":
self.my_bot = AppBuilderAPI(
app_token=self.config_data["app_token"],
history_enable=self.config_data["history_enable"]
)
self.app_builder_get_conversation_id()
except Exception as e:
logging.error(traceback.format_exc())


def app_builder_get_conversation_id(self):
try:
url = "https://qianfan.baidubce.com/v2/app/conversation"

payload = json.dumps({"app_id": self.config_data["app_id"]})
headers = {
'Content-Type': 'application/json',
'X-Appbuilder-Authorization': f'Bearer {self.config_data["app_token"]}'
}

logging.debug(f'payload={payload}\nheaders={headers}')

response = requests.request("POST", url, headers=headers, data=payload)
resp_json = json.loads(response.content)
if "conversation_id" in resp_json:
self.conversation_id = resp_json["conversation_id"]
logging.info(f"获取会话ID成功,会话ID为:{self.conversation_id}")
else:
logging.error(f"获取会话ID失败,请检查app_id/app_token是否正确。错误信息:{resp_json}")

return None
except Exception as e:
logging.error(traceback.format_exc())
logging.error(f"获取会话ID失败,请检查app_id/app_token是否正确。错误信息:{e}")
return None


def get_resp(self, prompt):
"""请求对应接口,获取返回值
Expand All @@ -70,6 +95,8 @@ def get_resp(self, prompt):
str: 返回的文本回答
"""
try:
resp_content = None

if self.config_data['type'] == "千帆大模型":
# create messages
messages: Messages = []
Expand Down Expand Up @@ -113,11 +140,32 @@ def get_resp(self, prompt):
self.history.append({"role": "assistant", "content": resp_content})
break
elif self.config_data['type'] == "AppBuilder":
resp_content = self.my_bot(
query=prompt,
response_mode="blocking"
)
url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

payload = json.dumps({
"app_id": self.config_data["app_id"],
"query": prompt,
"stream": False,
"conversation_id": self.conversation_id
})
headers = {
'Content-Type': 'application/json',
'X-Appbuilder-Authorization': f'Bearer {self.config_data["app_token"]}'
}

response = requests.request("POST", url, headers=headers, data=payload)
resp_json = json.loads(response.content)

logging.debug(f"resp_json={resp_json}")

if "content" in resp_json:
for data in resp_json["content"]:
if data["event_status"] == "done":
resp_content = data["outputs"]["text"]
else:
logging.error(f"获取LLM返回失败。{resp_json}")
return None

return resp_content

except Exception as e:
Expand All @@ -135,6 +183,8 @@ def get_resp(self, prompt):

data = {
"model": "ERNIEBot",
"app_id": "",
"app_token": "",
"api_key": "",
"secret_key": "",
"top_p": 0.8,
Expand Down
2 changes: 2 additions & 0 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,7 @@ def common_textarea_handle(content):
config_data["my_wenxinworkshop"]["history_enable"] = switch_my_wenxinworkshop_history_enable.value
config_data["my_wenxinworkshop"]["history_max_len"] = int(input_my_wenxinworkshop_history_max_len.value)

config_data["my_wenxinworkshop"]["app_id"] = input_my_wenxinworkshop_app_id.value
config_data["my_wenxinworkshop"]["app_token"] = input_my_wenxinworkshop_app_token.value

if config.get("webui", "show_card", "llm", "gemini"):
Expand Down Expand Up @@ -3790,6 +3791,7 @@ def save_config():
input_my_wenxinworkshop_top_p = ui.input(label='前p个选择', value=config.get("my_wenxinworkshop", "top_p"), placeholder='[0, 1.0] Nucleus采样。这个参数控制模型从累积概率大于一定阈值的令牌中进行采样。较高的值会产生更多的多样性,较低的值会产生更少但更确定的回答。').style("width:200px;")
input_my_wenxinworkshop_penalty_score = ui.input(label='惩罚得分', value=config.get("my_wenxinworkshop", "penalty_score"), placeholder='[1.0, 2.0] 在生成文本时对某些词语或模式施加的惩罚。这是一种调节生成内容的机制,用来减少或避免不希望出现的内容。').style("width:200px;")
with ui.row():
input_my_wenxinworkshop_app_id = ui.input(label='应用ID', value=config.get("my_wenxinworkshop", "app_id"), placeholder='千帆AppBuilder平台,个人空间 应用 应用ID').style("width:200px;")
input_my_wenxinworkshop_app_token = ui.input(label='app_token', value=config.get("my_wenxinworkshop", "app_token"), placeholder='千帆AppBuilder平台,我的应用-应用配置-发布详情-我的Agent应用-API调用,填入app_token').style("width:200px;")


Expand Down

0 comments on commit f769057

Please sign in to comment.