-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce0e714
commit 61f4c4c
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |