feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
35
backend/infra/contract/chatmodel/chat_model.go
Normal file
35
backend/infra/contract/chatmodel/chat_model.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package chatmodel
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/eino/components/model"
|
||||
)
|
||||
|
||||
//go:generate mockgen -destination ../../../internal/mock/infra/contract/chatmodel/base_model_mock.go -package mock -source ${GOPATH}/src/github.com/cloudwego/eino/components/model/interface.go BaseChatModel
|
||||
type BaseChatModel = model.BaseChatModel
|
||||
|
||||
//go:generate mockgen -destination ../../../internal/mock/infra/contract/chatmodel/toolcalling_model_mock.go -package mock -source ${GOPATH}/src/github.com/cloudwego/eino/components/model/interface.go ToolCallingChatModel
|
||||
type ToolCallingChatModel = model.ToolCallingChatModel
|
||||
|
||||
//go:generate mockgen -destination ../../../internal/mock/infra/contract/chatmodel/chat_model_factory_mock.go -package mock -source chat_model.go Factory
|
||||
type Factory interface {
|
||||
CreateChatModel(ctx context.Context, protocol Protocol, config *Config) (ToolCallingChatModel, error)
|
||||
SupportProtocol(protocol Protocol) bool
|
||||
}
|
||||
94
backend/infra/contract/chatmodel/config.go
Normal file
94
backend/infra/contract/chatmodel/config.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package chatmodel
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/eino-ext/components/model/deepseek"
|
||||
"github.com/cloudwego/eino-ext/libs/acl/openai"
|
||||
"google.golang.org/genai"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||
APIKey string `json:"api_key,omitempty" yaml:"api_key,omitempty"`
|
||||
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
|
||||
|
||||
Model string `json:"model" yaml:"model"`
|
||||
Temperature *float32 `json:"temperature,omitempty" yaml:"temperature,omitempty"`
|
||||
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty" yaml:"frequency_penalty,omitempty"`
|
||||
PresencePenalty *float32 `json:"presence_penalty,omitempty" yaml:"presence_penalty,omitempty"`
|
||||
MaxTokens *int `json:"max_tokens,omitempty" yaml:"max_tokens,omitempty"`
|
||||
TopP *float32 `json:"top_p,omitempty" yaml:"top_p"`
|
||||
TopK *int `json:"top_k,omitempty" yaml:"top_k"`
|
||||
Stop []string `json:"stop,omitempty" yaml:"stop"`
|
||||
EnableThinking *bool `json:"enable_thinking,omitempty" yaml:"enable_thinking,omitempty"`
|
||||
|
||||
OpenAI *OpenAIConfig `json:"open_ai,omitempty" yaml:"openai"`
|
||||
Claude *ClaudeConfig `json:"claude,omitempty" yaml:"claude"`
|
||||
Ark *ArkConfig `json:"ark,omitempty" yaml:"ark"`
|
||||
Deepseek *DeepseekConfig `json:"deepseek,omitempty" yaml:"deepseek"`
|
||||
Qwen *QwenConfig `json:"qwen,omitempty" yaml:"qwen"`
|
||||
Gemini *GeminiConfig `json:"gemini,omitempty" yaml:"gemini"`
|
||||
|
||||
Custom map[string]string `json:"custom,omitempty" yaml:"custom"`
|
||||
}
|
||||
|
||||
type OpenAIConfig struct {
|
||||
ByAzure bool `json:"by_azure,omitempty" yaml:"by_azure"`
|
||||
APIVersion string `json:"api_version,omitempty" yaml:"api_version"`
|
||||
|
||||
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty" yaml:"response_format"`
|
||||
}
|
||||
|
||||
type ClaudeConfig struct {
|
||||
ByBedrock bool `json:"by_bedrock" yaml:"by_bedrock"`
|
||||
// bedrock config
|
||||
AccessKey string `json:"access_key,omitempty" yaml:"access_key"`
|
||||
SecretAccessKey string `json:"secret_access_key,omitempty" yaml:"secret_access_key"`
|
||||
SessionToken string `json:"session_token,omitempty" yaml:"session_token"`
|
||||
Region string `json:"region,omitempty" yaml:"region"`
|
||||
}
|
||||
|
||||
type ArkConfig struct {
|
||||
Region string `json:"region" yaml:"region"`
|
||||
AccessKey string `json:"access_key,omitempty" yaml:"access_key"`
|
||||
SecretKey string `json:"secret_key,omitempty" yaml:"secret_key"`
|
||||
RetryTimes *int `json:"retry_times,omitempty" yaml:"retry_times"`
|
||||
CustomHeader map[string]string `json:"custom_header,omitempty" yaml:"custom_header"`
|
||||
}
|
||||
|
||||
type DeepseekConfig struct {
|
||||
ResponseFormatType deepseek.ResponseFormatType `json:"response_format_type" yaml:"response_format_type"`
|
||||
}
|
||||
|
||||
type QwenConfig struct {
|
||||
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty" yaml:"response_format"`
|
||||
}
|
||||
|
||||
type GeminiConfig struct {
|
||||
Backend genai.Backend `json:"backend,omitempty" yaml:"backend"`
|
||||
Project string `json:"project,omitempty" yaml:"project"`
|
||||
Location string `json:"location,omitempty" yaml:"location"`
|
||||
APIVersion string `json:"api_version,omitempty" yaml:"api_version"`
|
||||
Headers map[string][]string `json:"headers,omitempty" yaml:"headers"`
|
||||
TimeoutMs int64 `json:"timeout_ms,omitempty" yaml:"timeout_ms"`
|
||||
|
||||
IncludeThoughts *bool `json:"include_thoughts,omitempty" yaml:"include_thoughts"` // default true
|
||||
ThinkingBudget *int32 `json:"thinking_budget,omitempty" yaml:"thinking_budget"` // default nil
|
||||
}
|
||||
55
backend/infra/contract/chatmodel/protocol.go
Normal file
55
backend/infra/contract/chatmodel/protocol.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package chatmodel
|
||||
|
||||
import "github.com/coze-dev/coze-studio/backend/api/model/ocean/cloud/developer_api"
|
||||
|
||||
type Protocol string
|
||||
|
||||
const (
|
||||
ProtocolOpenAI Protocol = "openai"
|
||||
ProtocolClaude Protocol = "claude"
|
||||
ProtocolDeepseek Protocol = "deepseek"
|
||||
ProtocolGemini Protocol = "gemini"
|
||||
ProtocolArk Protocol = "ark"
|
||||
ProtocolOllama Protocol = "ollama"
|
||||
ProtocolQwen Protocol = "qwen"
|
||||
ProtocolErnie Protocol = "ernie"
|
||||
)
|
||||
|
||||
func (p Protocol) TOModelClass() developer_api.ModelClass {
|
||||
switch p {
|
||||
case ProtocolArk:
|
||||
return developer_api.ModelClass_SEED
|
||||
case ProtocolOpenAI:
|
||||
return developer_api.ModelClass_GPT
|
||||
case ProtocolDeepseek:
|
||||
return developer_api.ModelClass_DeekSeek
|
||||
case ProtocolClaude:
|
||||
return developer_api.ModelClass_Claude
|
||||
case ProtocolGemini:
|
||||
return developer_api.ModelClass_Gemini
|
||||
case ProtocolOllama:
|
||||
return developer_api.ModelClass_Llama
|
||||
case ProtocolQwen:
|
||||
return developer_api.ModelClass_QWen
|
||||
case ProtocolErnie:
|
||||
return developer_api.ModelClass_Ernie
|
||||
default:
|
||||
return developer_api.ModelClass_Other
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user