Skip to content

Commit

Permalink
refactor: aftermath from relocation of handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vein05 committed Feb 10, 2025
1 parent 2082940 commit 4d3d840
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 27 deletions.
3 changes: 1 addition & 2 deletions balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"

handlers "github.com/cohesion-org/deepseek-go/handlers"
utils "github.com/cohesion-org/deepseek-go/utils"
)

Expand All @@ -33,7 +32,7 @@ func GetBalance(c *Client, ctx context.Context) (*BalanceResponse, error) {
return nil, fmt.Errorf("error building request: %w", err)
}

resp, err := handlers.HandleNormalRequest(req)
resp, err := HandleNormalRequest(*c, req)

if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/cohesion-org/deepseek-go"
"github.com/cohesion-org/deepseek-go/constants"
handlers "github.com/cohesion-org/deepseek-go/handlers"
"github.com/cohesion-org/deepseek-go/internal/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -21,7 +20,7 @@ func TestCreateChatCompletion(t *testing.T) {
name string
req *deepseek.ChatCompletionRequest
wantErr bool
validateRes func(t *testing.T, res *handlers.ChatCompletionResponse)
validateRes func(t *testing.T, res *deepseek.ChatCompletionResponse)
}{
{
name: "basic completion",
Expand All @@ -32,7 +31,7 @@ func TestCreateChatCompletion(t *testing.T) {
},
},
wantErr: false,
validateRes: func(t *testing.T, res *handlers.ChatCompletionResponse) {
validateRes: func(t *testing.T, res *deepseek.ChatCompletionResponse) {
assert.NotEmpty(t, res.Choices[0].Message.Content)
},
},
Expand Down
9 changes: 4 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"context"
"fmt"

handlers "github.com/cohesion-org/deepseek-go/handlers"
utils "github.com/cohesion-org/deepseek-go/utils"
)

// CreateChatCompletion sends a chat completion request and returns the generated response.
func (c *Client) CreateChatCompletion(
ctx context.Context,
request *ChatCompletionRequest,
) (*handlers.ChatCompletionResponse, error) {
) (*ChatCompletionResponse, error) {
if request == nil {
return nil, fmt.Errorf("request cannot be nil")
}
Expand All @@ -27,7 +26,7 @@ func (c *Client) CreateChatCompletion(
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := handlers.HandleSendChatCompletionRequest(req)
resp, err := HandleSendChatCompletionRequest(*c, req)

if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
Expand All @@ -38,7 +37,7 @@ func (c *Client) CreateChatCompletion(
return nil, HandleAPIError(resp)
}

updatedResp, err := handlers.HandleChatCompletionResponse(resp)
updatedResp, err := HandleChatCompletionResponse(resp)

if err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
Expand All @@ -64,7 +63,7 @@ func (c *Client) CreateChatCompletionStream(
return nil, fmt.Errorf("error building request: %w", err)
}

resp, err := handlers.HandleSendChatCompletionRequest(req)
resp, err := HandleSendChatCompletionRequest(*c, req)
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"strings"

"github.com/cohesion-org/deepseek-go/handlers"
)

// JSONExtractor helps extract structured data from LLM responses
Expand All @@ -22,7 +20,7 @@ func NewJSONExtractor(schema json.RawMessage) *JSONExtractor {
}

// ExtractJSON attempts to extract and parse JSON from an LLM response
func (je *JSONExtractor) ExtractJSON(response *handlers.ChatCompletionResponse, target interface{}) error {
func (je *JSONExtractor) ExtractJSON(response *ChatCompletionResponse, target interface{}) error {
if response == nil {
return fmt.Errorf("response cannot be nil")
}
Expand Down
19 changes: 9 additions & 10 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"

"github.com/cohesion-org/deepseek-go/handlers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -20,7 +19,7 @@ func TestNewJSONExtractor(t *testing.T) {
func TestExtractJSON(t *testing.T) {
tests := []struct {
name string
response *handlers.ChatCompletionResponse
response *ChatCompletionResponse
schema json.RawMessage
target interface{}
expectError bool
Expand All @@ -35,19 +34,19 @@ func TestExtractJSON(t *testing.T) {
},
{
name: "Empty Choices",
response: &handlers.ChatCompletionResponse{
Choices: []handlers.Choice{},
response: &ChatCompletionResponse{
Choices: []Choice{},
},
schema: nil,
target: &struct{}{},
expectError: true,
},
{
name: "Valid JSON Response",
response: &handlers.ChatCompletionResponse{
Choices: []handlers.Choice{
response: &ChatCompletionResponse{
Choices: []Choice{
{
Message: handlers.Message{
Message: Message{
Content: `{"name": "test"}`,
},
},
Expand Down Expand Up @@ -278,10 +277,10 @@ func TestJSONExtractionEdgeCases(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
response := &handlers.ChatCompletionResponse{
Choices: []handlers.Choice{
response := &ChatCompletionResponse{
Choices: []Choice{
{
Message: handlers.Message{
Message: Message{
Content: tt.content,
},
},
Expand Down
3 changes: 1 addition & 2 deletions mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"

"github.com/cohesion-org/deepseek-go/constants"
handlers "github.com/cohesion-org/deepseek-go/handlers"
)

var validRoles = map[string]bool{
Expand All @@ -13,7 +12,7 @@ var validRoles = map[string]bool{
constants.ChatMessageRoleSystem: true,
}

func MapMessageToChatCompletionMessage(m handlers.Message) (ChatCompletionMessage, error) {
func MapMessageToChatCompletionMessage(m Message) (ChatCompletionMessage, error) {
if m.Role == "" {
return ChatCompletionMessage{}, errors.New("message role cannot be empty")
}
Expand Down
3 changes: 1 addition & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"

handlers "github.com/cohesion-org/deepseek-go/handlers"
utils "github.com/cohesion-org/deepseek-go/utils"
)

Expand Down Expand Up @@ -50,7 +49,7 @@ func ListAllModels(c *Client, ctx context.Context) (*APIModels, error) {
return nil, fmt.Errorf("error building request: %w", err)
}

resp, err := handlers.HandleNormalRequest(req)
resp, err := HandleNormalRequest(*c, req)

if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
Expand Down

0 comments on commit 4d3d840

Please sign in to comment.