DEEPSEEK_PASSWORD=your_password
DEEPSEEK_DEVICE_ID=your_device_id
DEEPSEEK_COOKIES=your_cookies
DEEPSEEK_DS_POW_RESPONSE=your_ds_pow_response

Python 腳本示例

以下是一個(gè)簡單的 Python 腳本,展示如何與 Chat DeepSeek R1 API 進(jìn)行交互:

import asyncio
import os
from deepseek_api import DeepseekAPI
from dotenv import load_dotenv
from deepseek_api.model import MessageData

load_dotenv()

async def main():
email = os.environ.get("DEEPSEEK_EMAIL")
password = os.environ.get("DEEPSEEK_PASSWORD")
device_id = os.environ.get("DEEPSEEK_DEVICE_ID")
cookies = os.environ.get("DEEPSEEK_COOKIES")
ds_pow_response = os.environ.get("DEEPSEEK_DS_POW_RESPONSE")

app = await DeepseekAPI.create(
email=email,
password=password,
save_login=True,
device_id=device_id,
custom_headers={
"cookie": cookies,
"x-ds-pow-response": ds_pow_response,
},
)

chat_session_id = await app.new_chat()
print(f"Starting chat session with id: {chat_session_id}")

message_id = None
async for chunk in app.chat(
message="who are you", id=chat_session_id, parent_message_id=message_id
):
chunk_data: MessageData = chunk
print(chunk_data.choices[0].delta.content, end="")
cur_message_id = chunk.get_message_id()
if not cur_message_id:
cur_message_id = 0
if not message_id or cur_message_id > message_id:
message_id = cur_message_id
print()

await app.close()

if __name__ == "__main__":
asyncio.run(main())

代碼說明

通過這個(gè)示例,你可以快速掌握 Chat DeepSeek R1 API 的基本使用方法,為進(jìn)一步的應(yīng)用開發(fā)奠定基礎(chǔ)。

與 Langchain 框架集成

為了進(jìn)一步擴(kuò)展 DeepSeek-R1 的應(yīng)用場景,我們可以將其與 Langchain 框架進(jìn)行集成。Langchain 是一個(gè)用于構(gòu)建語言模型的強(qiáng)大框架,通過集成可以實(shí)現(xiàn)更復(fù)雜的推理和交互功能。

集成步驟

  1. 配置環(huán)境變量
    確保 .env 文件中已正確填寫 DeepSeek 的憑證信息:

DEEPSEEK_EMAIL=your_email
DEEPSEEK_PASSWORD=your_password
DEEPSEEK_DEVICE_ID=your_device_id
DEEPSEEK_COOKIES=your_cookies
DEEPSEEK_DS_POW_RESPONSE=your_ds_pow_response
  1. 代碼示例
    以下是一個(gè)完整的代碼示例,展示如何將 Chat DeepSeek R1 API 與 Langchain 集成:

import asyncio
import os
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional
from dotenv import load_dotenv
from langchain_core.callbacks.manager import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM
from langchain_core.outputs import GenerationChunk
from chat_deepseek_api.model import MessageData
from chat_deepseek_api import DeepseekAPI

class ChatDeepSeekApiLLM(LLM):
email: str = None
password: str = None
device_id: str = None
cookies: str = None
ds_pow_response: str = None
app: DeepseekAPI = None
chat_session_id: str = None
message_id: int = 0

def __init__(self, email: str, password: str, device_id: str, cookies: str, ds_pow_response: str):
super(ChatDeepSeekApiLLM, self).__init__()
self.email = email
self.password = password
self.device_id = device_id
self.cookies = cookies
self.ds_pow_response = ds_pow_response

def _call(self, prompt: str, stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any) -> str:
if stop is not None:
raise ValueError("stop kwargs are not permitted.")
self._verify_config()
for message in self._generate_message(prompt):
chunk = GenerationChunk(text=message)
if run_manager:
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
return "".join([chunk for chunk in self._generate_message(prompt)])

async def _async_generate_message(self, prompt: str) -> AsyncIterator[str]:
if not self.app:
self.app = await DeepseekAPI.create(
email=self.email,
password=self.password,
save_login=True,
device_id=self.device_id,
custom_headers={
"cookie": self.cookies,
"x-ds-pow-response": self.ds_pow_response,
},
)
if not self.chat_session_id:
self.chat_session_id = await self.app.new_chat()
self.message_id = None
async for chunk in self.app.chat(
message=prompt, id=self.chat_session_id, parent_message_id=self.message_id
):
chunk_data: MessageData = chunk
yield chunk_data.choices[0].delta.content
cur_message_id = chunk.get_message_id()
if not cur_message_id:
cur_message_id = 0
if not self.message_id or cur_message_id > self.message_id:
self.message_id = cur_message_id

def _close(self) -> None:
if self.app:
loop = asyncio.get_event_loop()
loop.run_until_complete(self.app.close())

def _verify_config(self) -> None:
if not self.email:
raise ValueError("Email is required.")
if not self.password:
raise ValueError("Password is required.")
if not self.device_id:
raise ValueError("Device ID is required.")
if not self.cookies:
raise ValueError("Cookies are required.")
if not self.ds_pow_response:
raise ValueError("DS POW Response is required.")

if __name__ == "__main__":
load_dotenv()
email = os.environ.get("DEEPSEEK_EMAIL")
password = os.environ.get("DEEPSEEK_PASSWORD")
device_id = os.environ.get("DEEPSEEK_DEVICE_ID")
cookies = os.environ.get("DEEPSEEK_COOKIES")
ds_pow_response = os.environ.get("DEEPSEEK_DS_POW_RESPONSE")
model = ChatDeepSeekApiLLM(
email=email,
password=password,
device_id=device_id,
cookies=cookies,
ds_pow_response=ds_pow_response,
)
result = model.invoke("who are you")
print(result)
result = model.invoke("what can you do")
print(result)
model._close()

關(guān)鍵組件說明

通過這種集成方式,你可以利用 Langchain 構(gòu)建強(qiáng)大的應(yīng)用程序,充分發(fā)揮 DeepSeek-R1 的推理能力,提升應(yīng)用程序的功能性和響應(yīng)性。

結(jié)語

DeepSeek-R1 作為一款強(qiáng)大的推理模型,其透明性和可定制性使其在多個(gè)領(lǐng)域具有廣泛的應(yīng)用潛力。通過安裝 Chat DeepSeek R1 API,我們可以輕松地將其集成到各種應(yīng)用程序中。此外,與 Langchain 框架的結(jié)合進(jìn)一步擴(kuò)展了其應(yīng)用場景,使得開發(fā)者能夠構(gòu)建更加智能和高效的交互式應(yīng)用。

無論你是開發(fā)者、研究人員還是技術(shù)愛好者,DeepSeek-R1 都是一個(gè)值得探索的工具。它不僅能夠幫助你解決復(fù)雜的推理任務(wù),還能為你的項(xiàng)目帶來新的可能性。希望本文的介紹能夠幫助你更好地理解和使用 DeepSeek-R1,并激發(fā)你探索更多創(chuàng)新應(yīng)用的靈感。

上一篇:

深入探索 DeepSeek Janus Pro:本地部署與應(yīng)用實(shí)踐

下一篇:

DeepSeek API Key 密鑰獲取與應(yīng)用:開啟智能對話之旅
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場景實(shí)測,選對API

#AI文本生成大模型API

對比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對比試用API 限時(shí)免費(fèi)