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

Add deep seek support #242

Open
hadley opened this issue Dec 29, 2024 · 4 comments · May be fixed by #251
Open

Add deep seek support #242

hadley opened this issue Dec 29, 2024 · 4 comments · May be fixed by #251
Milestone

Comments

@hadley
Copy link
Member

hadley commented Dec 29, 2024

https://api-docs.deepseek.com/

@chendakeng
Copy link

chendakeng commented Jan 12, 2025

Thanks for your response to my issues.

Below are the issues that I encounter when using deepseek API:

The chat function is ok. But the custom function (e.g. ner, get sentiment) that ellmer created cannot work with deepseek. The deepseek document requires us to include explicit instruction of "use JSON format", that's why the system_promot is written like this. But it seems like the deepseek response has a different JSON format that the ellmer is supposed to handle.

This is the chat example:

> # Use the provider
> chat <- chat_openai(
+   base_url = "https://api.deepseek.com/v1",
+   api_key = "sk-xxx",
+   model = "deepseek-chat",
+   system_prompt = "You are a helpful assistant. Please provide responses in JSON format.",
+   api_args = list(
+     response_format = list(type = "json_object") 
+   )
+ )
> 
> 
> chat$chat("What is the capital of France?")
{
  "response": "The capital of France is Paris."
}

This is the sentiment example:

> text <- "
+   The product was okay, but the customer service was terrible. I probably
+   won't buy from them again.
+ "
> 
> type_sentiment <- type_object(
+   "Extract the sentiment scores of a given text. Sentiment scores should sum to 1.",
+   positive_score = type_number("Positive sentiment score, ranging from 0.0 to 1.0."),
+   negative_score = type_number("Negative sentiment score, ranging from 0.0 to 1.0."),
+   neutral_score = type_number("Neutral sentiment score, ranging from 0.0 to 1.0.")
+ )
> 
> 
> str(chat$extract_data(text, type = type_sentiment))
Error in S7_inherits(type, "TypeObject") : 
  `class` must be an <S7_class> or NULL

Then, I use httr2 to get the response directly:

> library(httr2)
> 
> # Function to make the request
> make_request <- function(api_key) {
+   request("https://api.deepseek.com/v1/chat/completions") |>
+     req_headers(
+       "Content-Type" = "application/json",
+       "Authorization" = paste("Bearer", api_key)
+     ) |>
+     req_body_json(list(
+       model = "deepseek-chat",
+       messages = list(
+         list(
+           role = "system",
+           content = "You are a helpful assistant. Please provide responses in JSON format."
+         ),
+         list(
+           role = "user", 
+           content = "Extract sentiment scores for this text with scores summing to 1.0: The product was okay, but the customer service was terrible."
+         )
+       ),
+       response_format = list(
+         type = "json_object"  # Changed this to match Deepseek's format
+       )
+     )) |>
+     req_perform() |>
+     resp_body_json()
+ }
> 
> # Use the function
> response <- `make_request("api-xxx")`
> str(response)
List of 7
 $ id                : chr "ebdebcbb-f3a8-4b44-a4c0-7ce55e2585b0"
 $ object            : chr "chat.completion"
 $ created           : int 1736663650
 $ model             : chr "deepseek-chat"
 $ choices           :List of 1
  ..$ :List of 4
  .. ..$ index        : int 0
  .. ..$ message      :List of 2
  .. .. ..$ role   : chr "assistant"
  .. .. ..$ content: chr "{\n  \"sentiment_scores\": {\n    \"positive\": 0.3,\n    \"neutral\": 0.4,\n    \"negative\": 0.3\n  }\n}"
  .. ..$ logprobs     : NULL
  .. ..$ finish_reason: chr "stop"
 $ usage             :List of 5
  ..$ prompt_tokens           : int 54
  ..$ completion_tokens       : int 43
  ..$ total_tokens            : int 97
  ..$ prompt_cache_hit_tokens : int 0
  ..$ prompt_cache_miss_tokens: int 54
 $ system_fingerprint: chr "fp_3a5770e1b4"

I hope this helps with your troubleshooting. Thanks again for developing such a wonderful package!

@hadley
Copy link
Member Author

hadley commented Jan 13, 2025

@chendakeng you're talking about structured data extraction? It looks like deep seek doesn't support that yet; just the older json_format convention (which elmer does not support).

hadley added a commit that referenced this issue Jan 13, 2025
@hadley hadley linked a pull request Jan 13, 2025 that will close this issue
@chendakeng
Copy link

Thanks for your reply. Yes, I am talking about structured data extraction.

By the way, how should I know if a specific model supports the method of structured data extraction that works with elmer?

@hadley
Copy link
Member Author

hadley commented Jan 14, 2025

From the ellmer docs or from the model docs? From the model docs, you're looking for something like "structured outputs" (e.g. https://openai.com/index/introducing-structured-outputs-in-the-api/). The underlying technology is pretty closely related to function/tool calling, so if there's good function calling support you can also abuse that to do structured data extraction (which is what chat_claude() does).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants