Deepseek API Usage with Langchain
In this article we’ll see how we can use the deepseek api from Lanchain framework as per documentation provided by Deepseek.
- Install following library. Remove single quotes in command prompt.
'!pip3 install langchain_openai' - Import BaseChatOpenAI class from the Langchain chat models
from langchain_openai.chat_models.base import BaseChatOpenAI -
Now create an BaseChatOpenAI object which will be used across our page.
llm = BaseChatOpenAI( model='deepseek-chat', openai_api_key='<Your API Key>' openai_api_base='https://api.deepseek.com', max_tokens=1024 ) - lets give an sample prompt message to check whether the api is working fine.
response = llm.invoke("Hi!") print(response.content)Hello! How can I assist you today? -
lets givea proper message and see the output to ensure that API is responding as expected
messages = [ ( "system", "You are a helpful assistant that translates English to Persian. Translate the user sentence.", ), ("human", "I love programming."), ] ai_msg = llm.invoke(messages) ai_msg.content - Check with complete message given to system
from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate( [ ( "system", "You are a helpful assistant that translates {input_language} to {output_language}.", ), ("human", "{input}"), ] ) chain = prompt | llm chain.invoke( { "input_language": "English", "output_language": "German", "input": "I love agentic AI.", } ) - Observe the output, esepcailly the KV Cache or content caching portion.
AIMessage(content='Ich liebe agentische KI.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 20, 'total_tokens': 27, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 20}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4', 'finish_reason': 'stop', 'logprobs': None}, id='run-46ca6f2a-21ef-45f4-83f6-814c07fab391-0', usage_metadata={'input_tokens': 20, 'output_tokens': 7, 'total_tokens': 27, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}) - You can find working google colab file in my github repo.
Enjoy Reading This Article?
Here are some more articles you might like to read next: