Skip to content

Commit

Permalink
feat: fake callback client added
Browse files Browse the repository at this point in the history
  • Loading branch information
nurali-techie committed Feb 4, 2025
1 parent ce0e714 commit 61f4c4c
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/81-fake-callback-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"context"
"fmt"
"os"

"github.com/go-deepseek/deepseek"
"github.com/go-deepseek/deepseek/request"
)

/*
Example Details:
Example shows how to use FakeCallbackClient to test user feature which is using go-deepseek client.
Here, Greeter() is user feature when invoke from main() needs deepseek client with DEEPSEEK_API_KEY
Check TestGreeter() in main_test.go file. It is testing user feature Greeter() using FakeCallbackClient. This does not need deepseek with DEEPSEEK_API_KEY.
Using FakeCallbackClient, you will be able to develop your feature and test your feature even though deepseek API is down.
*/

func main() {
client, err := deepseek.NewClient(os.Getenv("DEEPSEEK_API_KEY"))
if err != nil {
panic(err)
}
reply := Greeter(client, "Hello")
fmt.Println(reply)
}

func Greeter(client deepseek.Client, message string) string {
chatReq := &request.ChatCompletionsRequest{
Model: deepseek.DEEPSEEK_CHAT_MODEL,
Messages: []*request.Message{
{
Role: "user",
Content: message,
},
},
Stream: false,
}

resp, err := client.CallChatCompletionsChat(context.Background(), chatReq)
if err != nil {
panic(err)
}

reply := resp.Choices[0].Message.Content
return reply
}
56 changes: 56 additions & 0 deletions examples/81-fake-callback-client/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"testing"

"github.com/go-deepseek/deepseek/fake"
"github.com/go-deepseek/deepseek/request"
"github.com/go-deepseek/deepseek/response"
)

func TestGreeter(t *testing.T) {
callbacks := fake.Callbacks{}

callbacks.CallChatCompletionsChatCallback = func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error) {
if chatReq.Messages[0].Content == "Hello" {
chatResp := &response.ChatCompletionsResponse{
Choices: []*response.Choice{
{
Message: &response.Message{
Content: "How are you?",
},
},
},
}
return chatResp, nil
}

if chatReq.Messages[0].Content == "Bye" {
chatResp := &response.ChatCompletionsResponse{
Choices: []*response.Choice{
{
Message: &response.Message{
Content: "Good Day!",
},
},
},
}
return chatResp, nil
}

return nil, nil
}

client := fake.NewFakeCallbackClient(callbacks)

reply := Greeter(client, "Hello")
if reply != "How are you?" {
t.Fail()
}

reply = Greeter(client, "Bye")
if reply != "Good Day!" {
t.Fail()
}
}
64 changes: 64 additions & 0 deletions fake/callback_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fake

import (
"context"

"github.com/go-deepseek/deepseek/request"
"github.com/go-deepseek/deepseek/response"
)

type Callbacks struct {
CallChatCompletionsChatCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error)
CallChatCompletionsReasonerCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error)

StreamChatCompletionsChatCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error)
StreamChatCompletionsReasonerCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error)

PingChatCompletionsCallback func(ctx context.Context, inputMessage string) (outputMessge string, err error)
}

type FakeCallbackClient struct {
callbacks Callbacks
}

func NewFakeCallbackClient(callbacks Callbacks) *FakeCallbackClient {
fc := &FakeCallbackClient{
callbacks: callbacks,
}
return fc
}

func (c *FakeCallbackClient) CallChatCompletionsChat(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error) {
if c.callbacks.CallChatCompletionsChatCallback == nil {
panic("err: CallChatCompletionsChatCallback is nil")
}
return c.callbacks.CallChatCompletionsChatCallback(ctx, chatReq)
}

func (c *FakeCallbackClient) CallChatCompletionsReasoner(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error) {
if c.callbacks.CallChatCompletionsReasonerCallback == nil {
panic("err: CallChatCompletionsReasonerCallback is nil")
}
return c.callbacks.CallChatCompletionsReasonerCallback(ctx, chatReq)
}

func (c *FakeCallbackClient) StreamChatCompletionsChat(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error) {
if c.callbacks.StreamChatCompletionsChatCallback == nil {
panic("err: StreamChatCompletionsChatCallback is nil")
}
return c.callbacks.StreamChatCompletionsChatCallback(ctx, chatReq)
}

func (c *FakeCallbackClient) StreamChatCompletionsReasoner(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error) {
if c.callbacks.StreamChatCompletionsReasonerCallback == nil {
panic("err: StreamChatCompletionsReasonerCallback is nil")
}
return c.callbacks.StreamChatCompletionsReasonerCallback(ctx, chatReq)
}

func (c *FakeCallbackClient) PingChatCompletions(ctx context.Context, inputMessage string) (outputMessge string, err error) {
if c.callbacks.PingChatCompletionsCallback == nil {
panic("err: PingChatCompletionsCallback is nil")
}
return c.callbacks.PingChatCompletionsCallback(ctx, inputMessage)
}

0 comments on commit 61f4c4c

Please sign in to comment.