deepseek API调用

官网参考:https://api-docs.deepseek.com/zh-cn/guides/multi_round_chat

使用原因

  1. 需要gpu算力少。
  2. 精准度比较高。
  3. 中国的,不需要考虑vpn,国外手机注册的信息。
  4. 政府支持。
  5. 开源
    5.1 671B 100块a100
    5.2 7B 4090

可以私有化部署(为什么进行私有化部署)

在自己的机房的服务器中,进行deepseek部署。(政府,银行,国企电网)

  1. 安全性(数据可以不出自己的服务器)。
    1.1 直接使用自己的知识库。
    1.2 可以利用自己的数据进行微调训练。
  2. 拥有最高权限。
    2.1 没有次数限制
    2.2 没有网速限制
    2.3 第一掌控权

如果官方的deepseek api用不了怎么办

  1. 中转(淘宝 闲鱼 拼多多)
  2. 私有化部署

api key申请

  1. 登录后台
    登录地址:https://platform.deepseek.com/api_keys
  2. 创建api key
    在这里插入图片描述

api调用

针对代码不太擅长的,可以考虑安装marscode (https://www.marscode.com/extension?goInstall=true)

首次调用

调用模版如下

# 第一次调用需要用安装openai包
conda activate base
pip install openai 
# 具体服务器路径地址 /root/test_yang/deepseek
# python 调用
from openai import OpenAI

client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Hello"},
    ],
    stream=False
)

print(response.choices[0].message.content)

发生报错:

openai.APIStatusError: Error code: 402 - {'error': {'message': 'Insufficient Balance', 'type': 'unknown_error', 'param': None, 'code': 'invalid_request_error'}}

不必惊慌,只是因为你没有冲钱导致,充点费用就可以了。

api调用方式

在这里插入图片描述

推理模式

不考虑上下文,只针对提的问题进行回答
场景:代码生成
多次推理方式:

  1. 一个问题调用一次api
  2. 多问题同时问同时回
多轮对话

考虑上下文,此问题考虑上文
场景:角色扮演
多次推理方式:

  1. 一个问题调用一次api
  2. 多问题同时问同时回
其他官方文档会有更清晰的介绍

重点!应用

在这里插入图片描述

利用deepseek进行数据打标(提示词分类)

在这里插入图片描述

利用deepseek进行数据转结构化(json output)

在这里插入图片描述
在这里插入图片描述

利用deepseek进行数据“训练”

通过在提示词中多给一些准确案例,实现结果精准推理。比如在以下system_prompt中,多给一些EXAMPLE,出结果。

import json
from openai import OpenAI

client = OpenAI(
    api_key="<your api key>",
    base_url="https://api.deepseek.com",
)

system_prompt = """
The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format. 

EXAMPLE INPUT: 
Which is the highest mountain in the world? Mount Everest.

EXAMPLE JSON OUTPUT:
{
    "question": "Which is the highest mountain in the world?",
    "answer": "Mount Everest"
}
"""

user_prompt = "Which is the longest river in the world? The Nile River."

messages = [{"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    response_format={
        'type': 'json_object'
    }
)

print(json.loads(response.choices[0].message.content))
### 如何使用 LangChain 调用 DeepSeek API #### 使用 LangChain 进行集成或操作的具体方法 为了实现这一目标,可以利用 `requests` 库来发送 HTTP 请求给 DeepSeek API 并处理返回的数据。下面是一个简单的 Python 函数示例,展示了如何通过 LangChain 来调用 DeepSeek API: ```python import requests from langchain import LLM, PromptTemplate def call_deepseek_api(query_string): url = "https://api.deepseek.com/v1/search" payload = { 'query': query_string, 'size': 10 } headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' } response = requests.post(url, json=payload, headers=headers) if response.status_code == 200: results = response.json() return results['hits']['hits'] else: raise Exception(f"API request failed with status {response.status_code}") ``` 此函数接受查询字符串作为输入参数,并向 DeepSeek 发送 POST 请求以获取搜索结果[^1]。 接下来展示的是如何定义一个基于 LangChain 的提示模板 (Prompt Template),用于构建更复杂的交互逻辑: ```python template = """You are a helpful assistant that can search the web using DeepSeek. User asked: {question} To find an answer, you should use this function to interact with DeepSeek's API: call_deepseek_api("{question}"). Please provide me with relevant information based on what was found.""" prompt_template = PromptTemplate(input_variables=["question"], template=template) llm_chain = LLM(prompt_template) ``` 上述代码片段创建了一个自定义的语言模型链 (`LLM`) 实例,它可以根据用户的提问自动执行对 DeepSeek API调用来寻找答案。 最后,在实际应用中可能还需要考虑错误处理机制以及优化性能等方面的工作;此外,对于不同的业务场景也可以进一步扩展和完善这个基础框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值