当前位置: 首页 > wzjs >正文

重庆网站建设狐灵威海教育行业网站建设

重庆网站建设狐灵,威海教育行业网站建设,营销100个引流方案,综合门户型网站有哪些这篇文章是在 smolagents 官方教程结束后的番外篇,实现了如何使用 DeepSeek API Key CodeAgent 执行你的提示词。 之所以写这篇文章是因为 smolagents 没有提供 DeepSeek 的模型接口,尽管可以通过 HfApiModel 这个类来指定使用与 DeepSeek 相关的模型&…

这篇文章是在 smolagents 官方教程结束后的番外篇,实现了如何使用 DeepSeek API Key + CodeAgent 执行你的提示词。

之所以写这篇文章是因为 smolagents 没有提供 DeepSeek 的模型接口,尽管可以通过 HfApiModel 这个类来指定使用与 DeepSeek 相关的模型,但这些都不是直接使用 API 接口,因为 HuggingFace 官网的 DeepSeek-R1 页面直接写了下面这句话:

在这里插入图片描述

现在还不支持 HuggingFace 的 Tramsformers 调用,因此如果确实想通过 DeepSeek 的 API 接口调用模型,我们就需要自己实现一个类来为 CodeAgent 提供可操作的模型。


Step1. 购买 DeepSeek API Key

在日前(2025年02月27日,星期四)DeepSeek 官方已经重新恢复了 API Key 的购买,在此之前服务器爆满暂停了一段时间公开销售,登录 Platform 页面在完成实名认证后就可以直接购买:https://platform.deepseek.com/top_up。由于DeepSeek 是本土公司,所以在购买步骤上也更加人性化,可以直接使用支付宝或微信以预付的方式存放一些额度,不需要绑定信用卡也就不必担心自己将Token用超一夜破产的情况发生。

我购买的是最低额度的 10 元档位,根据使用经验直到我把代码调通总计消耗了不到 0.01 元的 Token,因此初学者也可以放心使用。

【注意】:这个API Key也是需要立即找一个地方记下来,后期也不能二次查看的
在这里插入图片描述


Step2. 自定义 DeepSeek 的Model

  • 官网链接:https://api-docs.deepseek.com/;

查看 DeepSeek API 平台官网中 Your First API Call 可以的发现API接口是兼容 OpenAI 格式的,我们需要参考官网demo编写自己的Model,首先要安装依赖:

【注意】demo中提到的 base_urlmodel 后期可以根据需求进行修改,官网提供了多个url与模型,但这里为了教学就用下面写的内容。

$ pip3 install openai

在这里插入图片描述

然后我们去查看 HfApiModel 这个类发现其通过继承 smolagents.Model 这个基类实现对 Qwen-Coder 模型的调用,并且至少需要对 __init____call__ 这两个成员函数进行重写:
在这里插入图片描述

最快的方法就是仿照其实现一个,这里直接给出我实现好的代码,我将这个写成一个文件后面就可以放在 smolagents 库里,未来就能直接通过 from somlagents import DeekSeekModel 的方式导入了:

  • DeepSeekModel.py
from smolagents import Model
from smolagents import Model, Tool
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from smolagents.models import parse_tool_args_if_needed, ChatMessage
from openai import OpenAIclass DeepSeekModel(Model):"""This model connects to [DeepSeek]("https://api.deepseek.com") as a gateway to hundreds of LLMs.Parameters:model_id (`str`):The model identifier to use on the server (e.g. "deepseek-chat").api_base (`str`, *optional*):The base URL of the OpenAI-compatible API server.api_key (`str`, *optional*):The API key to use for authentication.custom_role_conversions (`dict[str, str]`, *optional*):Custom role conversion mapping to convert message roles in others.Useful for specific models that do not support specific message roles like "system".**kwargs:Additional keyword arguments to pass to the OpenAI API."""def __init__(self,model_id: str = "deepseek-chat",api_base="https://api.deepseek.com",api_key=None,custom_role_conversions: Optional[Dict[str, str]] = None,**kwargs,):super().__init__(**kwargs)self.model_id = model_idself.api_base = api_baseself.api_key = api_keyself.custom_role_conversions = custom_role_conversionsself.flatten_messages_as_text = (kwargs.get("flatten_messages_as_text")if "flatten_messages_as_text" in kwargselse self.model_id.startswith(("ollama", "groq", "cerebras")))self.client = OpenAI(api_key=self.api_key, base_url=self.api_base)def __call__(self,messages: List[Dict[str, Any]],  # 允许 content 是 str 或 liststop_sequences: Optional[List[str]] = None,grammar: Optional[str] = None,tools_to_call_from: Optional[List[Tool]] = None,**kwargs,) -> ChatMessage:completion_kwargs = self._prepare_completion_kwargs(messages=messages,stop_sequences=stop_sequences,grammar=grammar,tools_to_call_from=tools_to_call_from,model=self.model_id,api_base=self.api_base,api_key=self.api_key,convert_images_to_image_urls=True,flatten_messages_as_text=self.flatten_messages_as_text,custom_role_conversions=self.custom_role_conversions,**kwargs,)def format_content(content):if isinstance(content, list):return "\n".join(map(str, content))  # 处理 list -> strreturn str(content)response = self.client.chat.completions.create(model=self.model_id,messages=[{"role": "system", "content": "You are a helpful assistant"},{"role": "user", "content": "\n".join(format_content(msg["content"]) for msg in messages)},],stream=False)      self.last_input_token_count = response.usage.prompt_tokensself.last_output_token_count = response.usage.completion_tokensmessage = ChatMessage.from_dict(response.choices[0].message.model_dump(include={"role", "content", "tool_calls"}))message.raw = response.choices[0].message.contentif tools_to_call_from is not None:return parse_tool_args_if_needed(message)return message

Step3. Agent调用

在实现了 DeepSeekModel 这个类后就可以直接编写Agent进行调用了,和之前使用 HfApiModel 完全一致:

from DeepSeekModel import DeepSeekModel
from smolagents import CodeAgentdeepseek_api_key = "你的DeepSeek API Key"model = DeepSeekModel(api_key=deepseek_api_key)
agent = CodeAgent(model=model,tools=[]
)
agent.max_steps = 3
agent.run("Could you give me the 118th number in the Fibonacci sequence?",)

运行如下:

$ python demo.py

可以看到 Agent 已经正确解析了我们自定义的模型,并将模型名 deepseek-chat 打印在控制台上:
在这里插入图片描述


文章转载自:

http://UIN1kpmz.qxrct.cn
http://1kPYb54x.qxrct.cn
http://tY21pw1f.qxrct.cn
http://r10R4U0Z.qxrct.cn
http://bSGqvbpE.qxrct.cn
http://QgqULOKY.qxrct.cn
http://WbUSDhaQ.qxrct.cn
http://NbyagTNN.qxrct.cn
http://LKDmx4kt.qxrct.cn
http://HgCQ7HdC.qxrct.cn
http://Yk89WtTS.qxrct.cn
http://c9LSWHAE.qxrct.cn
http://8WCFBOwX.qxrct.cn
http://jD3Z6IZR.qxrct.cn
http://vVIyvbFN.qxrct.cn
http://J7GpV4Bz.qxrct.cn
http://KxGcOCoI.qxrct.cn
http://o8MXf4R1.qxrct.cn
http://vfb4CxFG.qxrct.cn
http://LEc8OoEQ.qxrct.cn
http://YYYhVoZ1.qxrct.cn
http://fqy0gTjG.qxrct.cn
http://uqQTeOzg.qxrct.cn
http://GraezXeF.qxrct.cn
http://LaxgVz7R.qxrct.cn
http://Heq5tZyW.qxrct.cn
http://XX6ItgiQ.qxrct.cn
http://tNCv5R9w.qxrct.cn
http://BGizeYKk.qxrct.cn
http://SFd3meIF.qxrct.cn
http://www.dtcms.com/wzjs/684855.html

相关文章:

  • python做网站安全性电脑优化是什么意思
  • 上海中艺建设集团网站wordpress评论居中
  • 顶岗实践网站开发免费制作购物网站
  • 门户资源分享网站模板网易企业邮箱登录入口登录入口
  • 国外创意型网站设计电脑软件推广
  • 加盟网站制作推广wordpress网站下方
  • 网站建设 天津流程做网站
  • 做外贸的怎样才能上国外网站个人如何注册电商平台
  • 青岛高级网站建设价格免费资料网站网址下载
  • 地理位置地图网站建设网络广告策划与设计
  • 网站开发税率是多少wordpress类开源网站
  • 做qq图片的网站苏州建设公司有哪些
  • 怎么在自己做的网站上发视频北京最新消息今天上午
  • 上海品牌设计有限公司太原网站优化常识
  • 同城招聘网站自助建站wordpress限定ip
  • 缙云县城乡建设局网站页面设计要怎么做
  • 网站建设成都云免费外链发布平台
  • 视频剪辑教程自学网站做网站公司赚钱吗?
  • 凡科网站做网站多少钱网站优化seo教程
  • icp备案网站要先建好吗华为品牌vi设计
  • 购物网站的做网站制作网址
  • 企业网站建设 英铭中国佛山手机网站建设
  • 工信和信息化网站备案系统广西工商网站查询企业信息
  • 如何将百度地图加入网站谷城网站定制
  • 千博企业网站管理系统旗舰版保定网站建设方案
  • ip网站架设邹平 建设项目 网站公示
  • 漯河网站建设网站建设大学生网页设计怎么做
  • 珠海网站建设 金碟江西做网站的
  • 沈阳网站开发招聘发布网站域名设置
  • 上传 wordpress网站手机优化