Skip to content

Commit

Permalink
#60: reduce complexity of toModel func and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger12 committed Jan 18, 2024
1 parent 63272b0 commit 6d78447
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 64 deletions.
54 changes: 54 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,57 @@ const docTemplate = `{
}
},
"definitions": {
"anthropic.Config": {
"type": "object",
"required": [
"baseUrl",
"chatEndpoint",
"model"
],
"properties": {
"baseUrl": {
"type": "string"
},
"chatEndpoint": {
"type": "string"
},
"defaultParams": {
"$ref": "#/definitions/anthropic.Params"
},
"model": {
"type": "string"
}
}
},
"anthropic.Params": {
"type": "object",
"properties": {
"max_tokens": {
"type": "integer"
},
"metadata": {
"type": "string"
},
"stop": {
"type": "array",
"items": {
"type": "string"
}
},
"system": {
"type": "string"
},
"temperature": {
"type": "number"
},
"top_k": {
"type": "integer"
},
"top_p": {
"type": "number"
}
}
},
"azureopenai.Config": {
"type": "object",
"required": [
Expand Down Expand Up @@ -458,6 +509,9 @@ const docTemplate = `{
"id"
],
"properties": {
"anthropic": {
"$ref": "#/definitions/anthropic.Config"
},
"azureopenai": {
"$ref": "#/definitions/azureopenai.Config"
},
Expand Down
54 changes: 54 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,57 @@
}
},
"definitions": {
"anthropic.Config": {
"type": "object",
"required": [
"baseUrl",
"chatEndpoint",
"model"
],
"properties": {
"baseUrl": {
"type": "string"
},
"chatEndpoint": {
"type": "string"
},
"defaultParams": {
"$ref": "#/definitions/anthropic.Params"
},
"model": {
"type": "string"
}
}
},
"anthropic.Params": {
"type": "object",
"properties": {
"max_tokens": {
"type": "integer"
},
"metadata": {
"type": "string"
},
"stop": {
"type": "array",
"items": {
"type": "string"
}
},
"system": {
"type": "string"
},
"temperature": {
"type": "number"
},
"top_k": {
"type": "integer"
},
"top_p": {
"type": "number"
}
}
},
"azureopenai.Config": {
"type": "object",
"required": [
Expand Down Expand Up @@ -455,6 +506,9 @@
"id"
],
"properties": {
"anthropic": {
"$ref": "#/definitions/anthropic.Config"
},
"azureopenai": {
"$ref": "#/definitions/azureopenai.Config"
},
Expand Down
36 changes: 36 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
basePath: /
definitions:
anthropic.Config:
properties:
baseUrl:
type: string
chatEndpoint:
type: string
defaultParams:
$ref: '#/definitions/anthropic.Params'
model:
type: string
required:
- baseUrl
- chatEndpoint
- model
type: object
anthropic.Params:
properties:
max_tokens:
type: integer
metadata:
type: string
stop:
items:
type: string
type: array
system:
type: string
temperature:
type: number
top_k:
type: integer
top_p:
type: number
type: object
azureopenai.Config:
properties:
apiVersion:
Expand Down Expand Up @@ -224,6 +258,8 @@ definitions:
type: object
providers.LangModelConfig:
properties:
anthropic:
$ref: '#/definitions/anthropic.Config'
azureopenai:
$ref: '#/definitions/azureopenai.Config'
client:
Expand Down
20 changes: 10 additions & 10 deletions pkg/api/schemas/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type UnifiedChatResponse struct {
// ProviderResponse is the unified response from the provider.

type ProviderResponse struct {
SystemID map[string]string `json:"responseId,omitempty"`
SystemID map[string]string `json:"responseId,omitempty"`
Message ChatMessage `json:"message"`
TokenCount TokenCount `json:"tokenCount"`
}
Expand Down Expand Up @@ -146,16 +146,16 @@ type ConnectorsResponse struct {

// Anthropic Chat Response
type AnthropicChatCompletion struct {
ID string `json:"id"`
Type string `json:"type"`
Model string `json:"model"`
Role string `json:"role"`
Content []Content `json:"content"`
StopReason string `json:"stop_reason"`
StopSequence string `json:"stop_sequence"`
ID string `json:"id"`
Type string `json:"type"`
Model string `json:"model"`
Role string `json:"role"`
Content []Content `json:"content"`
StopReason string `json:"stop_reason"`
StopSequence string `json:"stop_sequence"`
}

type Content struct {
Type string `json:"type"`
Text string `json:"text"`
Type string `json:"type"`
Text string `json:"text"`
}
16 changes: 8 additions & 8 deletions pkg/providers/anthropic/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ type ChatRequest struct {
// NewChatRequestFromConfig fills the struct from the config. Not using reflection because of performance penalty it gives
func NewChatRequestFromConfig(cfg *Config) *ChatRequest {
return &ChatRequest{
Model: cfg.Model,
System: cfg.DefaultParams.System,
Temperature: cfg.DefaultParams.Temperature,
TopP: cfg.DefaultParams.TopP,
TopK: cfg.DefaultParams.TopK,
MaxTokens: cfg.DefaultParams.MaxTokens,
Metadata: cfg.DefaultParams.Metadata,
Model: cfg.Model,
System: cfg.DefaultParams.System,
Temperature: cfg.DefaultParams.Temperature,
TopP: cfg.DefaultParams.TopP,
TopK: cfg.DefaultParams.TopK,
MaxTokens: cfg.DefaultParams.MaxTokens,
Metadata: cfg.DefaultParams.Metadata,
StopSequences: cfg.DefaultParams.StopSequences,
Stream: false, // unsupported right now
Stream: false, // unsupported right now
}
}

Expand Down
68 changes: 68 additions & 0 deletions pkg/providers/anthropic/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package anthropic

import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

"glide/pkg/providers/clients"

"glide/pkg/api/schemas"

"glide/pkg/telemetry"

"github.com/stretchr/testify/require"
)

func TestOpenAIClient_ChatRequest(t *testing.T) {
// Anthropic Messages API: https://docs.anthropic.com/claude/reference/messages_post
AnthropicMock := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawPayload, _ := io.ReadAll(r.Body)

var data interface{}
// Parse the JSON body
err := json.Unmarshal(rawPayload, &data)
if err != nil {
t.Errorf("error decoding payload (%q): %v", string(rawPayload), err)
}

chatResponse, err := os.ReadFile(filepath.Clean("./testdata/chat.success.json"))
if err != nil {
t.Errorf("error reading openai chat mock response: %v", err)
}

w.Header().Set("Content-Type", "application/json")
_, err = w.Write(chatResponse)

if err != nil {
t.Errorf("error on sending chat response: %v", err)
}
})

AnthropicServer := httptest.NewServer(AnthropicMock)
defer AnthropicServer.Close()

ctx := context.Background()
providerCfg := DefaultConfig()
clientCfg := clients.DefaultClientConfig()

providerCfg.BaseURL = AnthropicServer.URL

client, err := NewClient(providerCfg, clientCfg, telemetry.NewTelemetryMock())
require.NoError(t, err)

request := schemas.UnifiedChatRequest{Message: schemas.ChatMessage{
Role: "human",
Content: "What's the biggest animal?",
}}

response, err := client.Chat(ctx, &request)
require.NoError(t, err)

require.Equal(t, "msg_013Zva2CMHLNnXjNJJKqJ2EF", response.ID)
}
12 changes: 12 additions & 0 deletions pkg/providers/anthropic/testdata/chat.req.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"model": "claude-instant-1.2",
"messages": [
{
"role": "human",
"content": "What's the biggest animal?"
}
],
"temperature": 1,
"top_p": 0,
"max_tokens": 100
}
14 changes: 14 additions & 0 deletions pkg/providers/anthropic/testdata/chat.success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message",
"model": "claude-2.1",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Blue is often seen as a calming and soothing color."
}
],
"stop_reason": "end_turn",
"stop_sequence": null
}
Loading

0 comments on commit 6d78447

Please sign in to comment.