From a1fc8a637b345969fb304d24d18044587ff1867d Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 23 Jun 2024 01:48:31 +0000 Subject: [PATCH] Include types in provider files, splitting wasn't ergonomic --- pkg/api/provider/google/google.go | 79 ++++++++++++++++++++++++++++++ pkg/api/provider/google/types.go | 80 ------------------------------- pkg/api/provider/openai/openai.go | 70 +++++++++++++++++++++++++++ pkg/api/provider/openai/types.go | 71 --------------------------- 4 files changed, 149 insertions(+), 151 deletions(-) delete mode 100644 pkg/api/provider/google/types.go delete mode 100644 pkg/api/provider/openai/types.go diff --git a/pkg/api/provider/google/google.go b/pkg/api/provider/google/google.go index 7c38ffb..a2c4b2e 100644 --- a/pkg/api/provider/google/google.go +++ b/pkg/api/provider/google/google.go @@ -13,6 +13,85 @@ import ( "git.mlow.ca/mlow/lmcli/pkg/api" ) +type Client struct { + APIKey string + BaseURL string +} + +type ContentPart struct { + Text string `json:"text,omitempty"` + FunctionCall *FunctionCall `json:"functionCall,omitempty"` + FunctionResp *FunctionResponse `json:"functionResponse,omitempty"` +} + +type FunctionCall struct { + Name string `json:"name"` + Args map[string]string `json:"args"` +} + +type FunctionResponse struct { + Name string `json:"name"` + Response interface{} `json:"response"` +} + +type Content struct { + Role string `json:"role"` + Parts []ContentPart `json:"parts"` +} + +type GenerationConfig struct { + MaxOutputTokens *int `json:"maxOutputTokens,omitempty"` + Temperature *float32 `json:"temperature,omitempty"` + TopP *float32 `json:"topP,omitempty"` + TopK *int `json:"topK,omitempty"` +} + +type GenerateContentRequest struct { + Contents []Content `json:"contents"` + Tools []Tool `json:"tools,omitempty"` + SystemInstruction *Content `json:"systemInstruction,omitempty"` + GenerationConfig *GenerationConfig `json:"generationConfig,omitempty"` +} + +type Candidate struct { + Content Content `json:"content"` + FinishReason string `json:"finishReason"` + Index int `json:"index"` +} + +type UsageMetadata struct { + PromptTokenCount int `json:"promptTokenCount"` + CandidatesTokenCount int `json:"candidatesTokenCount"` + TotalTokenCount int `json:"totalTokenCount"` +} + +type GenerateContentResponse struct { + Candidates []Candidate `json:"candidates"` + UsageMetadata UsageMetadata `json:"usageMetadata"` +} + +type Tool struct { + FunctionDeclarations []FunctionDeclaration `json:"functionDeclarations"` +} + +type FunctionDeclaration struct { + Name string `json:"name"` + Description string `json:"description"` + Parameters ToolParameters `json:"parameters"` +} + +type ToolParameters struct { + Type string `json:"type"` + Properties map[string]ToolParameter `json:"properties,omitempty"` + Required []string `json:"required,omitempty"` +} + +type ToolParameter struct { + Type string `json:"type"` + Description string `json:"description"` + Values []string `json:"values,omitempty"` +} + func convertTools(tools []api.ToolSpec) []Tool { geminiTools := make([]Tool, len(tools)) for i, tool := range tools { diff --git a/pkg/api/provider/google/types.go b/pkg/api/provider/google/types.go deleted file mode 100644 index 2e2bed4..0000000 --- a/pkg/api/provider/google/types.go +++ /dev/null @@ -1,80 +0,0 @@ -package google - -type Client struct { - APIKey string - BaseURL string -} - -type ContentPart struct { - Text string `json:"text,omitempty"` - FunctionCall *FunctionCall `json:"functionCall,omitempty"` - FunctionResp *FunctionResponse `json:"functionResponse,omitempty"` -} - -type FunctionCall struct { - Name string `json:"name"` - Args map[string]string `json:"args"` -} - -type FunctionResponse struct { - Name string `json:"name"` - Response interface{} `json:"response"` -} - -type Content struct { - Role string `json:"role"` - Parts []ContentPart `json:"parts"` -} - -type GenerationConfig struct { - MaxOutputTokens *int `json:"maxOutputTokens,omitempty"` - Temperature *float32 `json:"temperature,omitempty"` - TopP *float32 `json:"topP,omitempty"` - TopK *int `json:"topK,omitempty"` -} - -type GenerateContentRequest struct { - Contents []Content `json:"contents"` - Tools []Tool `json:"tools,omitempty"` - SystemInstruction *Content `json:"systemInstruction,omitempty"` - GenerationConfig *GenerationConfig `json:"generationConfig,omitempty"` -} - -type Candidate struct { - Content Content `json:"content"` - FinishReason string `json:"finishReason"` - Index int `json:"index"` -} - -type UsageMetadata struct { - PromptTokenCount int `json:"promptTokenCount"` - CandidatesTokenCount int `json:"candidatesTokenCount"` - TotalTokenCount int `json:"totalTokenCount"` -} - -type GenerateContentResponse struct { - Candidates []Candidate `json:"candidates"` - UsageMetadata UsageMetadata `json:"usageMetadata"` -} - -type Tool struct { - FunctionDeclarations []FunctionDeclaration `json:"functionDeclarations"` -} - -type FunctionDeclaration struct { - Name string `json:"name"` - Description string `json:"description"` - Parameters ToolParameters `json:"parameters"` -} - -type ToolParameters struct { - Type string `json:"type"` - Properties map[string]ToolParameter `json:"properties,omitempty"` - Required []string `json:"required,omitempty"` -} - -type ToolParameter struct { - Type string `json:"type"` - Description string `json:"description"` - Values []string `json:"values,omitempty"` -} diff --git a/pkg/api/provider/openai/openai.go b/pkg/api/provider/openai/openai.go index e2b010b..5e75231 100644 --- a/pkg/api/provider/openai/openai.go +++ b/pkg/api/provider/openai/openai.go @@ -13,6 +13,76 @@ import ( "git.mlow.ca/mlow/lmcli/pkg/api" ) +type OpenAIClient struct { + APIKey string + BaseURL string +} + +type ChatCompletionMessage struct { + Role string `json:"role"` + Content string `json:"content,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` + ToolCallID string `json:"tool_call_id,omitempty"` +} + +type ToolCall struct { + Type string `json:"type"` + ID string `json:"id"` + Index *int `json:"index,omitempty"` + Function FunctionDefinition `json:"function"` +} + +type FunctionDefinition struct { + Name string `json:"name"` + Description string `json:"description"` + Parameters ToolParameters `json:"parameters"` + Arguments string `json:"arguments,omitempty"` +} + +type ToolParameters struct { + Type string `json:"type"` + Properties map[string]ToolParameter `json:"properties,omitempty"` + Required []string `json:"required,omitempty"` +} + +type ToolParameter struct { + Type string `json:"type"` + Description string `json:"description"` + Enum []string `json:"enum,omitempty"` +} + +type Tool struct { + Type string `json:"type"` + Function FunctionDefinition `json:"function"` +} + +type ChatCompletionRequest struct { + Model string `json:"model"` + MaxTokens int `json:"max_tokens,omitempty"` + Temperature float32 `json:"temperature,omitempty"` + Messages []ChatCompletionMessage `json:"messages"` + N int `json:"n"` + Tools []Tool `json:"tools,omitempty"` + ToolChoice string `json:"tool_choice,omitempty"` + Stream bool `json:"stream,omitempty"` +} + +type ChatCompletionChoice struct { + Message ChatCompletionMessage `json:"message"` +} + +type ChatCompletionResponse struct { + Choices []ChatCompletionChoice `json:"choices"` +} + +type ChatCompletionStreamChoice struct { + Delta ChatCompletionMessage `json:"delta"` +} + +type ChatCompletionStreamResponse struct { + Choices []ChatCompletionStreamChoice `json:"choices"` +} + func convertTools(tools []api.ToolSpec) []Tool { openaiTools := make([]Tool, len(tools)) for i, tool := range tools { diff --git a/pkg/api/provider/openai/types.go b/pkg/api/provider/openai/types.go deleted file mode 100644 index 27eee03..0000000 --- a/pkg/api/provider/openai/types.go +++ /dev/null @@ -1,71 +0,0 @@ -package openai - -type OpenAIClient struct { - APIKey string - BaseURL string -} - -type ChatCompletionMessage struct { - Role string `json:"role"` - Content string `json:"content,omitempty"` - ToolCalls []ToolCall `json:"tool_calls,omitempty"` - ToolCallID string `json:"tool_call_id,omitempty"` -} - -type ToolCall struct { - Type string `json:"type"` - ID string `json:"id"` - Index *int `json:"index,omitempty"` - Function FunctionDefinition `json:"function"` -} - -type FunctionDefinition struct { - Name string `json:"name"` - Description string `json:"description"` - Parameters ToolParameters `json:"parameters"` - Arguments string `json:"arguments,omitempty"` -} - -type ToolParameters struct { - Type string `json:"type"` - Properties map[string]ToolParameter `json:"properties,omitempty"` - Required []string `json:"required,omitempty"` -} - -type ToolParameter struct { - Type string `json:"type"` - Description string `json:"description"` - Enum []string `json:"enum,omitempty"` -} - -type Tool struct { - Type string `json:"type"` - Function FunctionDefinition `json:"function"` -} - -type ChatCompletionRequest struct { - Model string `json:"model"` - MaxTokens int `json:"max_tokens,omitempty"` - Temperature float32 `json:"temperature,omitempty"` - Messages []ChatCompletionMessage `json:"messages"` - N int `json:"n"` - Tools []Tool `json:"tools,omitempty"` - ToolChoice string `json:"tool_choice,omitempty"` - Stream bool `json:"stream,omitempty"` -} - -type ChatCompletionChoice struct { - Message ChatCompletionMessage `json:"message"` -} - -type ChatCompletionResponse struct { - Choices []ChatCompletionChoice `json:"choices"` -} - -type ChatCompletionStreamChoice struct { - Delta ChatCompletionMessage `json:"delta"` -} - -type ChatCompletionStreamResponse struct { - Choices []ChatCompletionStreamChoice `json:"choices"` -}