Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: New Feature: deepseek connector #10293

Open
EngRajabi opened this issue Jan 25, 2025 · 12 comments
Open

.Net: New Feature: deepseek connector #10293

EngRajabi opened this issue Jan 25, 2025 · 12 comments
Assignees
Labels
ai connector Anything related to AI connectors .NET Issue or Pull requests regarding .NET code

Comments

@EngRajabi
Copy link


name: add deepseek connector
about: Deepseek is a great new service that has just been launched. Its prices are lower than openai. The R1 model even outperforms the O1 model.
site: https://www.deepseek.com/
api: https://api-docs.deepseek.com/
Image


@jamiemaguiredotnet
Copy link

A connector would be great.

I may use this on a project.

Thankyou.

@nmoeller
Copy link
Contributor

nmoeller commented Jan 26, 2025

I think you can already connect to deepSeek with the OpenAI Connector of Semantic Kernel.
The API seems to be compatible with the OpenAI Sdk, that means you could just change the BaseUrl to the Deepseek Url of the OpenAI Connector.

Check out this sample, where you can change the BaseUrl :
https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/local_models/lm_studio_chat_completion.py

@FrqSalah
Copy link

A connector to deepseek will be a big plus.

@EngRajabi
Copy link
Author

I checked this.
https://api-docs.deepseek.com/
It seems to be compatible with OpenAI's own SDK. But I haven't tested it.

@sophialagerkranspandey sophialagerkranspandey added .NET Issue or Pull requests regarding .NET code ai connector Anything related to AI connectors and removed triage labels Jan 27, 2025
@github-actions github-actions bot changed the title New Feature: deepseek connector .Net: New Feature: deepseek connector Jan 27, 2025
@markwallace-microsoft markwallace-microsoft moved this to Sprint: Planned in Semantic Kernel Jan 27, 2025
@markwallace-microsoft
Copy link
Member

@RogerBarreto do we have a .Net equivalent to this sample, where you can change the BaseUrl :
https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/local_models/lm_studio_chat_completion.py

@nmoeller
Copy link
Contributor

@markwallace-microsoft I think this could be the right example for dotnet : https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/AzureOpenAI_CustomClient.cs

But better @RogerBarreto verifies this, I am more in the Python SK Universe.

@roldengarm
Copy link

roldengarm commented Jan 27, 2025

I've tested it with the OpenAI connector, and it works fine for the deepseek-chat model 🚀 . Just like:

var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("deepseek-chat", new Uri("https://api.deepseek.com"), "sk-...");

However, with the reasoning model deepseek-reasoner, it does not work. After ~1 minute I'm getting:

The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 4 | BytePositionInLine: 0.
This is the first Reasoner model I use, so perhaps I'm doing something wrong.

@DavidWynnII
Copy link

I've tested it with the OpenAI connector, and it works fine for the deepseek-chat model 🚀 . Just like:

var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("deepseek-chat", new Uri("https://api.deepseek.com"), "sk-...");

However, with the reasoning model deepseek-reasoner, it does not work. After ~1 minute I'm getting:

The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 4 | BytePositionInLine: 0. This is the first Reasoner model I use, so perhaps I'm doing something wrong.

Also my first reasoning model, I too am getting the exact same error message.

@RogerBarreto
Copy link
Member

@roldengarm @DavidWynnII Thanks for the examples using the OpenAI Connector, I will be looking further how we can have a working example with the reasoner.

@RogerBarreto RogerBarreto moved this from Sprint: Planned to Sprint: In Progress in Semantic Kernel Jan 28, 2025
@RogerBarreto
Copy link
Member

RogerBarreto commented Jan 28, 2025

I'm suspicious this is an error in the API for the moment, even querying it with Postman, I'm getting empty response after 60 seconds, might be related. Suggest waiting for the API to get back into a stable state.

My result: 200 OK, without any content after exactly 60 seconds.

Image

https://status.deepseek.com/

As of now: 28-JAN 21:00 (UTC)
Image

@babaktaremi
Copy link

I recommend prioritizing API resiliency as the primary focus for the new DeepSeek connector, given the current unreliability of the deepseek services.

@RogerBarreto
Copy link
Member

RogerBarreto commented Jan 29, 2025

Ok, managed to test it against the API.

As I could check the OpenAI connector was enough to work against the DeepSeek API.

Important

DeepSeek documentation makes use as well of the openai library in Your First API Call with Python
Which suggest they should be fully functional used against the OpenAI Connector.

If you want to capture the additional reasoning information from the message follow the guidance below with an extra breaking glass approach to capture the thinking (reasoning):

Setup (Reasoner)

var kernel = Kernel.CreateBuilder()
    .AddOpenAIChatCompletion("deepseek-reasoner", new Uri("https://api.deepseek.com"), "sk-...")
    .Build();

// Retrieve your chatService from the kernel
var service = kernel.GetRequiredService<IChatCompletionService>();

var chatHistory = new ChatHistory("You are a librarian, expert about books");
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");

Non-Streaming

// Get the reply from deep seek
var reply = await chatService.GetChatMessageContentAsync(chatHistory);

// Getting the reasoning
var jsonContent = JsonNode.Parse(ModelReaderWriter.Write(reply.InnerContent!));
var reasoning = jsonContent!["choices"]![0]!["message"]!["reasoning_content"];

Streaming

await foreach (var update in chatService.GetStreamingChatMessageContentsAsync(chatHistory))
{
    var jsonContent = JsonNode.Parse(ModelReaderWriter.Write(update.InnerContent!));
    var reasoningUpdate = jsonContent!["choices"]![0]!["delta"]!["reasoning_content"];
    Console.Write(update.Content);
    Console.WriteLine(reasoningUpdate);
}

As the OpenAI Connector is sufficient to retrieve the information initially the above approach may unblock you. We will discuss internally the need to add a connector or keep this as our recommendation.

Also keep in mind that DeepSeek can run locally with the Ollama Connector (OllamaSharp).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai connector Anything related to AI connectors .NET Issue or Pull requests regarding .NET code
Projects
Status: Sprint: In Progress
Development

No branches or pull requests

10 participants