Skip to content

Commit

Permalink
GO-4459 Export return type
Browse files Browse the repository at this point in the history
  • Loading branch information
jmetrikat committed Nov 16, 2024
1 parent a4c6665 commit 7383699
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions util/ai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
)

type AI interface {
WritingTools(ctx context.Context, params *pb.RpcAIWritingToolsRequest) (result, error)
WritingTools(ctx context.Context, params *pb.RpcAIWritingToolsRequest) (Result, error)
app.ComponentRunnable
}

Expand All @@ -54,7 +54,7 @@ type PromptConfig struct {
JSONMode bool
}

type result struct {
type Result struct {
Answer string
}

Expand All @@ -78,7 +78,7 @@ func (ai *AIService) Close(_ context.Context) (err error) {
return nil
}

func (ai *AIService) WritingTools(ctx context.Context, params *pb.RpcAIWritingToolsRequest) (result, error) {
func (ai *AIService) WritingTools(ctx context.Context, params *pb.RpcAIWritingToolsRequest) (Result, error) {
ai.mu.Lock()
defer ai.mu.Unlock()

Expand All @@ -95,7 +95,7 @@ func (ai *AIService) WritingTools(ctx context.Context, params *pb.RpcAIWritingTo

if language, exists := detector.DetectLanguageOf(text); !exists {
log.Errorf("unsupported language detected: %s", language)
return result{}, fmt.Errorf("%w: %s", ErrUnsupportedLanguage, language)
return Result{}, fmt.Errorf("%w: %s", ErrUnsupportedLanguage, language)
}
}

Expand All @@ -117,18 +117,18 @@ func (ai *AIService) WritingTools(ctx context.Context, params *pb.RpcAIWritingTo

answer, err := ai.chat(context.Background())
if err != nil {
return result{}, err
return Result{}, err
}

// extract answer value from json response, except for default mode
if params.Mode != 0 {
extractedAnswer, err := ai.extractAnswerByMode(answer)
if err != nil {
return result{}, err
return Result{}, err
}

return result{Answer: extractedAnswer}, nil
return Result{Answer: extractedAnswer}, nil
}

return result{Answer: answer}, nil
return Result{Answer: answer}, nil
}
18 changes: 9 additions & 9 deletions util/ai/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestWritingTools(t *testing.T) {
tests := []struct {
name string
params *pb.RpcAIWritingToolsRequest
validateResult func(t *testing.T, result result, err error)
validateResult func(t *testing.T, result Result, err error)
}{
{
name: "SupportedLanguage",
Expand All @@ -39,7 +39,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0.1,
Text: "This is a test.",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.NoError(t, err)
},
},
Expand All @@ -55,7 +55,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0.1,
Text: "Съешь ещё этих мягких французских булок, да выпей же чаю. Впрочем, одних слов недостаточно для демонстрации эффекта, но этот текст подходит для большинства задач. Здесь можно написать что угодно, и никто не обратит внимания на содержание.",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.Error(t, err)
assert.True(t, errors.Is(err, ErrUnsupportedLanguage))
},
Expand All @@ -72,7 +72,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0.1,
Text: "Can you use an invalid endpoint?",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.Error(t, err)
assert.True(t, errors.Is(err, ErrEndpointNotReachable))
},
Expand All @@ -89,7 +89,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0.1,
Text: "Can you use an invalid model?",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.Error(t, err)
assert.True(t, errors.Is(err, ErrModelNotFound))
},
Expand All @@ -106,7 +106,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0.1,
Text: "Can you use an invalid token?",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.Error(t, err)
assert.True(t, errors.Is(err, ErrAuthRequired))
},
Expand All @@ -123,7 +123,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0,
Text: "What is the capital of France?",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.NoError(t, err)
assert.NotEmpty(t, result.Answer)
assert.Contains(t, result.Answer, "Paris")
Expand All @@ -141,7 +141,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0,
Text: "What is the capital of France?",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.NoError(t, err)
assert.NotEmpty(t, result.Answer)
assert.Contains(t, result.Answer, "Paris")
Expand All @@ -159,7 +159,7 @@ func TestWritingTools(t *testing.T) {
Temperature: 0,
Text: "Countries, Capitals\nFrance, Paris\nGermany, Berlin",
},
validateResult: func(t *testing.T, result result, err error) {
validateResult: func(t *testing.T, result Result, err error) {
assert.NoError(t, err)
assert.NotEmpty(t, result.Answer)
assert.Equal(t, "| Country | Capital |\n|----------|---------|\n| France | Paris |\n| Germany | Berlin |\n", result.Answer)
Expand Down

0 comments on commit 7383699

Please sign in to comment.