Skip to content

Commit 61f4c4c

Browse files
committed
feat: fake callback client added
1 parent ce0e714 commit 61f4c4c

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/go-deepseek/deepseek"
9+
"github.com/go-deepseek/deepseek/request"
10+
)
11+
12+
/*
13+
Example Details:
14+
15+
Example shows how to use FakeCallbackClient to test user feature which is using go-deepseek client.
16+
17+
Here, Greeter() is user feature when invoke from main() needs deepseek client with DEEPSEEK_API_KEY
18+
19+
Check TestGreeter() in main_test.go file. It is testing user feature Greeter() using FakeCallbackClient. This does not need deepseek with DEEPSEEK_API_KEY.
20+
21+
Using FakeCallbackClient, you will be able to develop your feature and test your feature even though deepseek API is down.
22+
*/
23+
24+
func main() {
25+
client, err := deepseek.NewClient(os.Getenv("DEEPSEEK_API_KEY"))
26+
if err != nil {
27+
panic(err)
28+
}
29+
reply := Greeter(client, "Hello")
30+
fmt.Println(reply)
31+
}
32+
33+
func Greeter(client deepseek.Client, message string) string {
34+
chatReq := &request.ChatCompletionsRequest{
35+
Model: deepseek.DEEPSEEK_CHAT_MODEL,
36+
Messages: []*request.Message{
37+
{
38+
Role: "user",
39+
Content: message,
40+
},
41+
},
42+
Stream: false,
43+
}
44+
45+
resp, err := client.CallChatCompletionsChat(context.Background(), chatReq)
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
reply := resp.Choices[0].Message.Content
51+
return reply
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/go-deepseek/deepseek/fake"
8+
"github.com/go-deepseek/deepseek/request"
9+
"github.com/go-deepseek/deepseek/response"
10+
)
11+
12+
func TestGreeter(t *testing.T) {
13+
callbacks := fake.Callbacks{}
14+
15+
callbacks.CallChatCompletionsChatCallback = func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error) {
16+
if chatReq.Messages[0].Content == "Hello" {
17+
chatResp := &response.ChatCompletionsResponse{
18+
Choices: []*response.Choice{
19+
{
20+
Message: &response.Message{
21+
Content: "How are you?",
22+
},
23+
},
24+
},
25+
}
26+
return chatResp, nil
27+
}
28+
29+
if chatReq.Messages[0].Content == "Bye" {
30+
chatResp := &response.ChatCompletionsResponse{
31+
Choices: []*response.Choice{
32+
{
33+
Message: &response.Message{
34+
Content: "Good Day!",
35+
},
36+
},
37+
},
38+
}
39+
return chatResp, nil
40+
}
41+
42+
return nil, nil
43+
}
44+
45+
client := fake.NewFakeCallbackClient(callbacks)
46+
47+
reply := Greeter(client, "Hello")
48+
if reply != "How are you?" {
49+
t.Fail()
50+
}
51+
52+
reply = Greeter(client, "Bye")
53+
if reply != "Good Day!" {
54+
t.Fail()
55+
}
56+
}

fake/callback_client.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package fake
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-deepseek/deepseek/request"
7+
"github.com/go-deepseek/deepseek/response"
8+
)
9+
10+
type Callbacks struct {
11+
CallChatCompletionsChatCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error)
12+
CallChatCompletionsReasonerCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error)
13+
14+
StreamChatCompletionsChatCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error)
15+
StreamChatCompletionsReasonerCallback func(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error)
16+
17+
PingChatCompletionsCallback func(ctx context.Context, inputMessage string) (outputMessge string, err error)
18+
}
19+
20+
type FakeCallbackClient struct {
21+
callbacks Callbacks
22+
}
23+
24+
func NewFakeCallbackClient(callbacks Callbacks) *FakeCallbackClient {
25+
fc := &FakeCallbackClient{
26+
callbacks: callbacks,
27+
}
28+
return fc
29+
}
30+
31+
func (c *FakeCallbackClient) CallChatCompletionsChat(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error) {
32+
if c.callbacks.CallChatCompletionsChatCallback == nil {
33+
panic("err: CallChatCompletionsChatCallback is nil")
34+
}
35+
return c.callbacks.CallChatCompletionsChatCallback(ctx, chatReq)
36+
}
37+
38+
func (c *FakeCallbackClient) CallChatCompletionsReasoner(ctx context.Context, chatReq *request.ChatCompletionsRequest) (*response.ChatCompletionsResponse, error) {
39+
if c.callbacks.CallChatCompletionsReasonerCallback == nil {
40+
panic("err: CallChatCompletionsReasonerCallback is nil")
41+
}
42+
return c.callbacks.CallChatCompletionsReasonerCallback(ctx, chatReq)
43+
}
44+
45+
func (c *FakeCallbackClient) StreamChatCompletionsChat(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error) {
46+
if c.callbacks.StreamChatCompletionsChatCallback == nil {
47+
panic("err: StreamChatCompletionsChatCallback is nil")
48+
}
49+
return c.callbacks.StreamChatCompletionsChatCallback(ctx, chatReq)
50+
}
51+
52+
func (c *FakeCallbackClient) StreamChatCompletionsReasoner(ctx context.Context, chatReq *request.ChatCompletionsRequest) (response.StreamReader, error) {
53+
if c.callbacks.StreamChatCompletionsReasonerCallback == nil {
54+
panic("err: StreamChatCompletionsReasonerCallback is nil")
55+
}
56+
return c.callbacks.StreamChatCompletionsReasonerCallback(ctx, chatReq)
57+
}
58+
59+
func (c *FakeCallbackClient) PingChatCompletions(ctx context.Context, inputMessage string) (outputMessge string, err error) {
60+
if c.callbacks.PingChatCompletionsCallback == nil {
61+
panic("err: PingChatCompletionsCallback is nil")
62+
}
63+
return c.callbacks.PingChatCompletionsCallback(ctx, inputMessage)
64+
}

0 commit comments

Comments
 (0)