-
Notifications
You must be signed in to change notification settings - Fork 199
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
colorfulandcjy0806
wants to merge
7
commits into
InternLM:main
Choose a base branch
from
colorfulandcjy0806:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
基于Lagent新版的内容,完成了一个利用浦语API完成的Agent。实现了CustomAPILLM 类,该类继承自 BaseAPILLM,封装了对 API 的调用逻辑,然后利用Streamlit启动Web服务,能够调用Arxiv检索工具。
原代码中,res['content'] 和 role_prompt['content'] 被直接拼接,可能在非字符串(如列表)场景下导致运行时错误。 修改后增加类型检查和安全处理,逻辑检查将非字符串转化为字符串
Harold-lkk
reviewed
Nov 21, 2024
examples/run_agent_api_web_demo.py
Outdated
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] |
There was a problem hiding this comment.
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里
There was a problem hiding this comment.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
基于Lagent新版内容,通过定义的GPTAPI类调用浦语API,并利用Streamlit框架启动Web服务,能够集成并调用Arxiv文献检索工具。
11.22更新,侧边栏功能全部可用,同时给出了硅基流动API的使用例子。