Skip to content

Commit

Permalink
fix: update qwen/openai version
Browse files Browse the repository at this point in the history
  • Loading branch information
meguminnnnnnnnn committed Jan 14, 2025
1 parent d65fad8 commit b100145
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 74 deletions.
52 changes: 40 additions & 12 deletions components/embedding/openai/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,53 @@ import (
)

type EmbeddingConfig struct {
// if you want to use Azure OpenAI Service, set the next three fields. refs: https://learn.microsoft.com/en-us/azure/ai-services/openai/
// ByAzure set this field to true when using Azure OpenAI Service, otherwise it does not need to be set.
// Timeout specifies the maximum duration to wait for API responses
// Optional. Default: no timeout
Timeout time.Duration `json:"timeout"`

// APIKey is your authentication key
// Use OpenAI API key or Azure API key depending on the service
// Required
APIKey string `json:"api_key"`

// HTTPClient is used to send HTTP requests
// Optional. Default: http.DefaultClient
HTTPClient *http.Client

// The following three fields are only required when using Azure OpenAI Service, otherwise they can be ignored.
// For more details, see: https://learn.microsoft.com/en-us/azure/ai-services/openai/

// ByAzure indicates whether to use Azure OpenAI Service
// Required for Azure
ByAzure bool `json:"by_azure"`
// BaseURL https://{{$YOUR_RESOURCE_NAME}}.openai.azure.com, YOUR_RESOURCE_NAME is the name of your resource that you have created on Azure.

// BaseURL is the Azure OpenAI endpoint URL
// Format: https://{YOUR_RESOURCE_NAME}.openai.azure.com. YOUR_RESOURCE_NAME is the name of your resource that you have created on Azure.
// Required for Azure
BaseURL string `json:"base_url"`
// APIVersion specifies the API version you want to use.

// APIVersion specifies the Azure OpenAI API version
// Required for Azure
APIVersion string `json:"api_version"`

// APIKey is typically OPENAI_API_KEY, but if you have set up Azure, then it is Azure API_KEY.
APIKey string `json:"api_key"`
// The following fields correspond to OpenAI's chat completion API parameters
//Ref: https://platform.openai.com/docs/api-reference/embeddings/create

// Timeout specifies the http request timeout.
Timeout time.Duration `json:"timeout"`
// Model specifies the ID of the model to use for embedding generation
// Required
Model string `json:"model"`

// The following fields have the same meaning as the fields in the openai embedding API request. Ref: https://platform.openai.com/docs/api-reference/embeddings/create
Model string `json:"model"`
// EncodingFormat specifies the format of the embeddings output
// Optional. Default: EmbeddingEncodingFormatFloat
EncodingFormat *openai.EmbeddingEncodingFormat `json:"encoding_format,omitempty"`
Dimensions *int `json:"dimensions,omitempty"`
User *string `json:"user,omitempty"`

// Dimensions specifies the number of dimensions the resulting output embeddings should have
// Optional. Only supported in text-embedding-3 and later models
Dimensions *int `json:"dimensions,omitempty"`

// User is a unique identifier representing your end-user
// Optional. Helps OpenAI monitor and detect abuse
User *string `json:"user,omitempty"`
}

var _ embedding.Embedder = (*Embedder)(nil)
Expand Down
2 changes: 1 addition & 1 deletion components/embedding/openai/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/bytedance/mockey v1.2.13
github.com/cloudwego/eino v0.3.4
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c
github.com/sashabaranov/go-openai v1.32.5
)

Expand Down
4 changes: 2 additions & 2 deletions components/embedding/openai/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/eino v0.3.4 h1:trWw8lKU1t1b7PMKSW1GXEJ4H2rLiGWFyVoMJJ3pRDg=
github.com/cloudwego/eino v0.3.4/go.mod h1:+kmJimGEcKuSI6OKhet7kBedkm1WUZS3H1QRazxgWUo=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a h1:gHpod7vRkLDi4otZjY9pMom35Rk0/kCDbPl2smyjcK0=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a/go.mod h1:uUhE8oadYHiRwU8bymmyYK58TYgc5qm794PdyXoGJlo=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c h1:Qu7VVK7BP8bTcWwFB1t1fe8Ee3WYZNGgxf33rdqotnA=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c/go.mod h1:H5UK9sjotuBaZCO6CFvPRaYDBBfRz/MSFR9ZNEmwgCo=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
101 changes: 73 additions & 28 deletions components/model/openai/chatmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,83 @@ import (
)

type ChatModelConfig struct {
// if you want to use Azure OpenAI Service, set the next three fields. refs: https://learn.microsoft.com/en-us/azure/ai-services/openai/
// ByAzure set this field to true when using Azure OpenAI Service, otherwise it does not need to be set.
ByAzure bool `json:"by_azure"`
// BaseURL https://{{$YOUR_RESOURCE_NAME}}.openai.azure.com, YOUR_RESOURCE_NAME is the name of your resource that you have created on Azure.
BaseURL string `json:"base_url"`
// APIVersion specifies the API version you want to use.
APIVersion string `json:"api_version"`

// APIKey is typically OPENAI_API_KEY, but if you have set up Azure, then it is Azure API_KEY.
// APIKey is your authentication key
// Use OpenAI API key or Azure API key depending on the service
// Required
APIKey string `json:"api_key"`

// Timeout specifies the http request timeout.
// Timeout specifies the maximum duration to wait for API responses
// Optional. Default: no timeout
Timeout time.Duration `json:"timeout"`

// The following fields have the same meaning as the fields in the openai chat completion API request. Ref: https://platform.openai.com/docs/api-reference/chat/create
Model string `json:"model"`
MaxTokens *int `json:"max_tokens,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"top_p,omitempty"`
N *int `json:"n,omitempty"`
Stop []string `json:"stop,omitempty"`
PresencePenalty *float32 `json:"presence_penalty,omitempty"`
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty"`
Seed *int `json:"seed,omitempty"`
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`
LogitBias map[string]int `json:"logit_bias,omitempty"`
LogProbs *bool `json:"logprobs,omitempty"`
TopLogProbs *int `json:"top_logprobs,omitempty"`
User *string `json:"user,omitempty"`
// The following three fields are only required when using Azure OpenAI Service, otherwise they can be ignored.
// For more details, see: https://learn.microsoft.com/en-us/azure/ai-services/openai/

// ByAzure indicates whether to use Azure OpenAI Service
// Required for Azure
ByAzure bool `json:"by_azure"`

// BaseURL is the Azure OpenAI endpoint URL
// Format: https://{YOUR_RESOURCE_NAME}.openai.azure.com. YOUR_RESOURCE_NAME is the name of your resource that you have created on Azure.
// Required for Azure
BaseURL string `json:"base_url"`

// APIVersion specifies the Azure OpenAI API version
// Required for Azure
APIVersion string `json:"api_version"`

// The following fields correspond to OpenAI's chat completion API parameters
// Ref: https://platform.openai.com/docs/api-reference/chat/create

// Model specifies the ID of the model to use
// Required
Model string `json:"model"`

// MaxTokens limits the maximum number of tokens that can be generated in the chat completion
// Optional. Default: model's maximum
MaxTokens *int `json:"max_tokens,omitempty"`

// Temperature specifies what sampling temperature to use
// Generally recommend altering this or TopP but not both.
// Range: 0.0 to 2.0. Higher values make output more random
// Optional. Default: 1.0
Temperature *float32 `json:"temperature,omitempty"`

// TopP controls diversity via nucleus sampling
// Generally recommend altering this or Temperature but not both.
// Range: 0.0 to 1.0. Lower values make output more focused
// Optional. Default: 1.0
TopP *float32 `json:"top_p,omitempty"`

// Stop sequences where the API will stop generating further tokens
// Optional. Example: []string{"\n", "User:"}
Stop []string `json:"stop,omitempty"`

// PresencePenalty prevents repetition by penalizing tokens based on presence
// Range: -2.0 to 2.0. Positive values increase likelihood of new topics
// Optional. Default: 0
PresencePenalty *float32 `json:"presence_penalty,omitempty"`

// ResponseFormat specifies the format of the model's response
// Optional. Use for structured outputs
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty"`

// Seed enables deterministic sampling for consistent outputs
// Optional. Set for reproducible results
Seed *int `json:"seed,omitempty"`

// FrequencyPenalty prevents repetition by penalizing tokens based on frequency
// Range: -2.0 to 2.0. Positive values decrease likelihood of repetition
// Optional. Default: 0
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`

// LogitBias modifies likelihood of specific tokens appearing in completion
// Optional. Map token IDs to bias values from -100 to 100
LogitBias map[string]int `json:"logit_bias,omitempty"`

// User unique identifier representing end-user
// Optional. Helps OpenAI monitor and detect abuse
User *string `json:"user,omitempty"`
}

var _ model.ChatModel = (*ChatModel)(nil)
Expand All @@ -78,15 +126,12 @@ func NewChatModel(ctx context.Context, config *ChatModelConfig) (*ChatModel, err
MaxTokens: config.MaxTokens,
Temperature: config.Temperature,
TopP: config.TopP,
N: config.N,
Stop: config.Stop,
PresencePenalty: config.PresencePenalty,
ResponseFormat: config.ResponseFormat,
Seed: config.Seed,
FrequencyPenalty: config.FrequencyPenalty,
LogitBias: config.LogitBias,
LogProbs: config.LogProbs,
TopLogProbs: config.TopLogProbs,
User: config.User,
}
}
Expand Down
3 changes: 0 additions & 3 deletions components/model/openai/chatmodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func TestOpenAIGenerate(t *testing.T) {
MaxTokens: &expectedRequestBody.MaxTokens,
Temperature: &expectedRequestBody.Temperature,
TopP: &expectedRequestBody.TopP,
N: &expectedRequestBody.N,
Stop: expectedRequestBody.Stop,
PresencePenalty: &expectedRequestBody.PresencePenalty,
ResponseFormat: &protocol.ChatCompletionResponseFormat{
Expand All @@ -181,8 +180,6 @@ func TestOpenAIGenerate(t *testing.T) {
Seed: expectedRequestBody.Seed,
FrequencyPenalty: &expectedRequestBody.FrequencyPenalty,
LogitBias: expectedRequestBody.LogitBias,
LogProbs: &expectedRequestBody.LogProbs,
TopLogProbs: &expectedRequestBody.TopLogProbs,
User: &expectedRequestBody.User,
}

Expand Down
2 changes: 0 additions & 2 deletions components/model/openai/examples/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ import (
func main() {
accessKey := os.Getenv("OPENAI_API_KEY")

N := 3
ctx := context.Background()
chatModel, err := openai.NewChatModel(ctx, &openai.ChatModelConfig{
BaseURL: "https://api.openai.com/v1",
N: &N,
APIKey: accessKey,
ByAzure: true,
Model: "gpt-4o-2024-05-13",
Expand Down
2 changes: 1 addition & 1 deletion components/model/openai/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/bytedance/mockey v1.2.13
github.com/cloudwego/eino v0.3.4
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c
github.com/getkin/kin-openapi v0.118.0
github.com/sashabaranov/go-openai v1.32.5
)
Expand Down
4 changes: 2 additions & 2 deletions components/model/openai/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/eino v0.3.4 h1:trWw8lKU1t1b7PMKSW1GXEJ4H2rLiGWFyVoMJJ3pRDg=
github.com/cloudwego/eino v0.3.4/go.mod h1:+kmJimGEcKuSI6OKhet7kBedkm1WUZS3H1QRazxgWUo=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a h1:gHpod7vRkLDi4otZjY9pMom35Rk0/kCDbPl2smyjcK0=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a/go.mod h1:uUhE8oadYHiRwU8bymmyYK58TYgc5qm794PdyXoGJlo=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c h1:Qu7VVK7BP8bTcWwFB1t1fe8Ee3WYZNGgxf33rdqotnA=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c/go.mod h1:H5UK9sjotuBaZCO6CFvPRaYDBBfRz/MSFR9ZNEmwgCo=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
84 changes: 64 additions & 20 deletions components/model/qwen/chatmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,72 @@ import (
// https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api?spm=a2c4g.11186623.help-menu-2400256.d_3_3_0.c3b24823WzuCqJ&scm=20140722.H_2712576._.OR_help-T_cn-DAS-zh-V_1
// https://help.aliyun.com/zh/model-studio/developer-reference/compatibility-of-openai-with-dashscope?spm=a2c4g.11186623.0.i49
type ChatModelConfig struct {
BaseURL string `json:"base_url"` // 公有云: https://dashscope.aliyuncs.com/compatible-mode/v1
APIKey string `json:"api_key"`
// Timeout specifies the http request timeout.

// APIKey is your authentication key
// Use OpenAI API key or Azure API key depending on the service
// Required
APIKey string `json:"api_key"`

// Timeout specifies the maximum duration to wait for API responses
// Optional. Default: no timeout
Timeout time.Duration `json:"timeout"`

// The following fields have the same meaning as the fields in the openai chat completion API request. Ref: https://platform.openai.com/docs/api-reference/chat/create
// Model list see: https://help.aliyun.com/zh/model-studio/getting-started/models
Model string `json:"model"`
MaxTokens *int `json:"max_tokens,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"top_p,omitempty"`
Stop []string `json:"stop,omitempty"`
PresencePenalty *float32 `json:"presence_penalty,omitempty"`
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty"`
Seed *int `json:"seed,omitempty"`
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`
LogitBias map[string]int `json:"logit_bias,omitempty"`
LogProbs *bool `json:"logprobs,omitempty"`
TopLogProbs *int `json:"top_logprobs,omitempty"`
User *string `json:"user,omitempty"`
// BaseURL specifies the QWen endpoint URL
// Required. Example: https://dashscope.aliyuncs.com/compatible-mode/v1
BaseURL string `json:"base_url"`

// The following fields correspond to OpenAI's chat completion API parameters
// Ref: https://platform.openai.com/docs/api-reference/chat/create

// Model specifies the ID of the model to use
// Required
Model string `json:"model"`

// MaxTokens limits the maximum number of tokens that can be generated in the chat completion
// Optional. Default: model's maximum
MaxTokens *int `json:"max_tokens,omitempty"`

// Temperature specifies what sampling temperature to use
// Generally recommend altering this or TopP but not both.
// Range: 0.0 to 2.0. Higher values make output more random
// Optional. Default: 1.0
Temperature *float32 `json:"temperature,omitempty"`

// TopP controls diversity via nucleus sampling
// Generally recommend altering this or Temperature but not both.
// Range: 0.0 to 1.0. Lower values make output more focused
// Optional. Default: 1.0
TopP *float32 `json:"top_p,omitempty"`

// Stop sequences where the API will stop generating further tokens
// Optional. Example: []string{"\n", "User:"}
Stop []string `json:"stop,omitempty"`

// PresencePenalty prevents repetition by penalizing tokens based on presence
// Range: -2.0 to 2.0. Positive values increase likelihood of new topics
// Optional. Default: 0
PresencePenalty *float32 `json:"presence_penalty,omitempty"`

// ResponseFormat specifies the format of the model's response
// Optional. Use for structured outputs
ResponseFormat *openai.ChatCompletionResponseFormat `json:"response_format,omitempty"`

// Seed enables deterministic sampling for consistent outputs
// Optional. Set for reproducible results
Seed *int `json:"seed,omitempty"`

// FrequencyPenalty prevents repetition by penalizing tokens based on frequency
// Range: -2.0 to 2.0. Positive values decrease likelihood of repetition
// Optional. Default: 0
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`

// LogitBias modifies likelihood of specific tokens appearing in completion
// Optional. Map token IDs to bias values from -100 to 100
LogitBias map[string]int `json:"logit_bias,omitempty"`

// User unique identifier representing end-user
// Optional. Helps OpenAI monitor and detect abuse
User *string `json:"user,omitempty"`
}

type ChatModel struct {
Expand All @@ -76,8 +122,6 @@ func NewChatModel(ctx context.Context, config *ChatModelConfig) (*ChatModel, err
Seed: config.Seed,
FrequencyPenalty: config.FrequencyPenalty,
LogitBias: config.LogitBias,
LogProbs: config.LogProbs,
TopLogProbs: config.TopLogProbs,
User: config.User,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion components/model/qwen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/bytedance/mockey v1.2.13
github.com/cloudwego/eino v0.3.4
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c
github.com/smartystreets/goconvey v1.8.1
)

Expand Down
4 changes: 2 additions & 2 deletions components/model/qwen/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/eino v0.3.4 h1:trWw8lKU1t1b7PMKSW1GXEJ4H2rLiGWFyVoMJJ3pRDg=
github.com/cloudwego/eino v0.3.4/go.mod h1:+kmJimGEcKuSI6OKhet7kBedkm1WUZS3H1QRazxgWUo=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a h1:gHpod7vRkLDi4otZjY9pMom35Rk0/kCDbPl2smyjcK0=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250106073650-ed838398894a/go.mod h1:uUhE8oadYHiRwU8bymmyYK58TYgc5qm794PdyXoGJlo=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c h1:Qu7VVK7BP8bTcWwFB1t1fe8Ee3WYZNGgxf33rdqotnA=
github.com/cloudwego/eino-ext/libs/acl/openai v0.0.0-20250114170443-d65fad8dcc3c/go.mod h1:H5UK9sjotuBaZCO6CFvPRaYDBBfRz/MSFR9ZNEmwgCo=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit b100145

Please sign in to comment.