Skip to content

Commit

Permalink
feat: add StreamingWithReasoningContent function to handle chat compl…
Browse files Browse the repository at this point in the history
…etion with reasoning
  • Loading branch information
Vein05 committed Feb 14, 2025
1 parent 821ce20 commit af2ebee
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/chat_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,52 @@ func Streaming() {
}
log.Println("The full message is: ", fullMessage)
}

// streaing with ReasoningContent
func StreamingWithReasoningContent() {
client := deepseek.NewClient(os.Getenv("DEEPSEEK_API_KEY"))
request := &deepseek.StreamChatCompletionRequest{
Model: deepseek.DeepSeekReasoner,
Messages: []deepseek.ChatCompletionMessage{
{Role: constants.ChatMessageRoleUser, Content: "Hello, how are you?"},
},
Stream: true,
}
ctx := context.Background()

stream, err := client.CreateChatCompletionStream(ctx, request)
if err != nil {
log.Fatalf("ChatCompletionStream error: %v", err)
}

var fullMessage string
var fullReasoning string
defer stream.Close()
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
fmt.Println("\nStream finished")
break
}
if err != nil {
fmt.Printf("\nStream error: %v\n", err)
break
}
for _, choice := range response.Choices {
fullMessage += choice.Delta.Content // Accumulate chunk content
fullReasoning += choice.Delta.ReasoningContent // Accumulate chunk reasoning content
if choice.Delta.ReasoningContent != "" {
log.Println("Reasoning: ", choice.Delta.ReasoningContent)
}
if choice.Delta.Content != "" {
log.Println("Content:", choice.Delta.Content)
}
}
if streamUsage := response.Usage; streamUsage != nil && streamUsage.TotalTokens > 0 {
log.Printf("Prompt tokens: %d, Completion tokens: %d, Total tokens: %d",
streamUsage.PromptTokens, streamUsage.CompletionTokens, streamUsage.TotalTokens)
}
}
log.Println("Full message: ", fullMessage)
log.Println("\nFull reasoning: ", fullReasoning)
}

0 comments on commit af2ebee

Please sign in to comment.