DeepSeek R1 is a large language model (LLM) focused on reasoning and code generation, with a large context window intended for handling extended interactions. This repo provides code examples demonstrating its use in Azure AI Foundry with Azure AI Python SDK / LangChain.
- Pre-requisites
- Scenario 1: Logical Puzzle
- Scenario 2: Financial Loan Approval
- Scenario 3: RAG for Quantum Computing in Arxiv
- Deploy DeepSeek R1 from Azure AI Foundry's model catalog as a serverless API. Specifics of the deployment process is described here.
- Add required environment variable:
Environment Variable | Description |
---|---|
AZURE_FOUNDRY_DEEPSEEK |
Deployment name of the DeepSeek R1 model |
- Install the required Python packages using the pip command and the provided requirements.txt file:
pip install -r requirements.txt
Note
Jupyter notebooks utilise the DefaultAzureCredential class. Depending on your environment, the Python code will search for available Azure identities in the order described here.
- Initialise Azure AI Inference client with DeepSeek R1's deployment endpoint and Azure credentails:
client = ChatCompletionsClient(
endpoint = DS_Endpoint,
credential = Az_Credential
)
- complete function allows you to pass system / user messages, enable streaming, restrict volume of the model's output (completion) and configure other inference parameters:
response = client.complete(
messages = [
System_Message,
User_Message,
],
max_tokens = 2048,
stream = True
)
Note
User prompt: Alice, Bob and Carol each have a different favourite colour: red, blue or green. Alice doesn't like red. Bob's favourite colour is not blue. Carol's favourite colour is red. What is each person's favourite colour? Explain your reasoning step by step.
- Define DeepSeek R1 model as an LLM for LangChain:
model = AzureAIChatCompletionsModel(
endpoint = DS_Endpoint,
credential = Az_Credential
)
- Define loan approval requirements as a Prompt Template for Langchain:
loan_approval_prompt = PromptTemplate(
input_variables = ["applicant_info"],
template = """
...
"""
)
- Configure and invoke the chain
chain = loan_approval_prompt | model
chain.invoke(applicant_info)
Note
System prompt: You are a financial advisor at a bank. You need to decide whether to approve a loan application. Consider the following applicant information: {applicant_info}. Provide your decision (approve or deny) AND a detailed explanation of your reasoning, including specific factors you considered and how they influenced your decision. Be transparent and thorough, as this is for a regulated industry.
- Define DeepSeek R1 model as an LLM for LangChain:
model = AzureAIChatCompletionsModel(
endpoint = DS_Endpoint,
credential = Az_Credential
)
- Define Arxiv document retrieval
arxiv_retriever = ArxivRetriever(
load_max_docs=2,
get_ful_documents=True,
)
- Define research paper analysis as a Prompt Template for Langchain:
prompt_template = PromptTemplate(
input_variables = ["query", "relevant_info"],
template = """
...
"""
)
- Configure and invoke the chain
chain = prompt_template | model
relevant_papers = arxiv_retriever.invoke(query)
relevant_info = "\n".join([paper.page_content for paper in relevant_papers])
chain.invoke({"query": query, "relevant_info": relevant_info})
Note
System prompt: You are an expert scientific researcher. Use the following information from arXiv to answer the user's question. If there is no sufficient information, say 'I need more information to answer this question'. Question: {query} Relevant Information: {relevant_info} Answer: