Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create run_agent_api_web_demo.py #275

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

colorfulandcjy0806
Copy link

@colorfulandcjy0806 colorfulandcjy0806 commented Nov 20, 2024

基于Lagent新版内容,通过定义的GPTAPI类调用浦语API,并利用Streamlit框架启动Web服务,能够集成并调用Arxiv文献检索工具。
11.22更新,侧边栏功能全部可用,同时给出了硅基流动API的使用例子。

基于Lagent新版的内容,完成了一个利用浦语API完成的Agent。实现了CustomAPILLM 类,该类继承自 BaseAPILLM,封装了对 API 的调用逻辑,然后利用Streamlit启动Web服务,能够调用Arxiv检索工具。
原代码中,res['content'] 和 role_prompt['content'] 被直接拼接,可能在非字符串(如列表)场景下导致运行时错误。
修改后增加类型检查和安全处理,逻辑检查将非字符串转化为字符串
Comment on lines 18 to 59
class CustomAPILLM(BaseAPILLM):
"""自定义的 API LLM 类,用于调用外部 API 进行文本生成。"""

def __init__(self, model_type, meta_template=None, **gen_params):
super().__init__(model_type, meta_template=meta_template, **gen_params)

def call_api(self, messages):
"""调用外部 API 并返回响应结果。"""
url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
"Authorization": "Bearer " + YOUR_TOKEN_HERE
}
data = {
"model": self.model_type,
"messages": messages,
"n": 1,
"temperature": self.gen_params['temperature'],
"top_p": self.gen_params['top_p'],
"stream": False,
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"API 调用失败,状态码: {response.status_code}")

def generate(self, inputs: Union[str, List[str]], **gen_params) -> Union[str, List[str]]:
"""调用外部 API。"""
if isinstance(inputs, str):
inputs = [{"role": "user", "content": inputs}]
elif isinstance(inputs, list) and isinstance(inputs[0], str):
inputs = [{"role": "user", "content": text} for text in inputs]

# 调用 call_api 并返回响应
response = self.call_api(inputs)
content = response["choices"][0]["message"]["content"]

if len(inputs) == 1:
return content
else:
return [content]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接移动到 lagents/llms/puyu.py 类名换成puyuAPI,把对baseAPI里边的修改可以都移动到这个api里

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经修改了,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants