Skip to content

Commit

Permalink
bump go-openai version
Browse files Browse the repository at this point in the history
fix relay-gin embedding
  • Loading branch information
LemonHX committed Mar 1, 2024
1 parent 9ce5c5e commit 8dd91f1
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 63 deletions.
16 changes: 5 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ module go.limit.dev/unollm
go 1.21.4

require (
github.com/KevinZonda/GoX v0.0.15
github.com/Limit-LAB/go-gemini v0.0.6
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.3.1
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1
github.com/joho/godotenv v1.5.1
github.com/sashabaranov/go-openai v1.17.8
golang.org/x/crypto v0.12.0
github.com/pkoukk/tiktoken-go v0.1.6
github.com/sashabaranov/go-openai v1.20.2
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
gorm.io/driver/mysql v1.5.2
gorm.io/gorm v1.25.5
)

require (
Expand All @@ -27,22 +22,21 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkoukk/tiktoken-go v0.1.6 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down
21 changes: 2 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
github.com/KevinZonda/GoX v0.0.15 h1:u+G41/srg8c6bmI6Oqw9iBwYiSQk9FKp9fv/YBZadFk=
github.com/KevinZonda/GoX v0.0.15/go.mod h1:WHV3YUyG+ou3wuUgwO4SRhMiKxLTVK5inajmtR1MUXo=
github.com/Limit-LAB/go-gemini v0.0.5 h1:LGYgw6jKPeLFDiNanplQoNfEJRpxZaubS1G+vovqjB4=
github.com/Limit-LAB/go-gemini v0.0.5/go.mod h1:GFozl8Ugxjni4xZgKXPXYuctHiAHpZrCbduHcW5yii4=
github.com/Limit-LAB/go-gemini v0.0.6 h1:67YVcM1dgV0HZkZ1AKjdmxGFn2OV5g3cUvLpICmIrh4=
github.com/Limit-LAB/go-gemini v0.0.6/go.mod h1:GFozl8Ugxjni4xZgKXPXYuctHiAHpZrCbduHcW5yii4=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand Down Expand Up @@ -37,8 +33,6 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
Expand All @@ -51,12 +45,6 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1 h1:HcUWd006luQPljE73d5sk+/VgYPGUReEVz2y1/qylwY=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1/go.mod h1:w9Y7gY31krpLmrVU5ZPG9H7l9fZuRu5/3R3S3FMtVQ4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down Expand Up @@ -94,8 +82,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/sashabaranov/go-openai v1.17.8 h1:snuE7l0XQ1KAmkY/cODAEgxu2fl+g/ybXK6cKQzli/E=
github.com/sashabaranov/go-openai v1.17.8/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sashabaranov/go-openai v1.20.2 h1:nilzF2EKzaHyK4Rk2Dbu/aJEZbtIvskDIXvfS4yx+6M=
github.com/sashabaranov/go-openai v1.20.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down Expand Up @@ -158,9 +146,4 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
2 changes: 1 addition & 1 deletion grpcServer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package grpcServer

import (
"github.com/Limit-LAB/go-gemini"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/provider/Baichuan"
"go.limit.dev/unollm/provider/ChatGLM"
Expand Down
10 changes: 6 additions & 4 deletions grpcServer/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"log"

"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/relay"
"go.limit.dev/unollm/relay/reqTransformer"
Expand Down Expand Up @@ -47,14 +47,15 @@ func OpenAIChatCompletionStreaming(cli *openai.Client, rs *model.LLMRequestSchem
}

type myEmbeddingRequest struct {
Text string
Text string
Model string
}

func (emr myEmbeddingRequest) Convert() openai.EmbeddingRequest {

return openai.EmbeddingRequest{
Input: emr.Text,
Model: 17,
Model: openai.EmbeddingModel(emr.Model),
EncodingFormat: openai.EmbeddingEncodingFormatFloat,
}
}
Expand All @@ -63,7 +64,8 @@ func OpenAIEmbeddingRequest(cli *openai.Client, req *model.EmbeddingRequest) (*m
log.Println("OPENAI_LLM_API")

res, err := cli.CreateEmbeddings(context.Background(), myEmbeddingRequest{
Text: req.Text,
Text: req.Text,
Model: req.EmbeddingRequestInfo.Model,
})
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
Expand Down
4 changes: 2 additions & 2 deletions grpcServer/relay_function_calling_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"time"

"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/provider/ChatGLM"
)
Expand Down Expand Up @@ -34,7 +34,7 @@ func functionCallingRequestMake(req *model.LLMRequestSchema) bool {
for i, f := range info.Functions {
tools[i] = openai.Tool{
Type: "function",
Function: openai.FunctionDefinition{
Function: &openai.FunctionDefinition{
Name: f.Name,
Description: f.Description,
Parameters: map[string]any{
Expand Down
28 changes: 17 additions & 11 deletions grpcServer/relay_gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/relay"
"go.limit.dev/unollm/relay/reqTransformer"
Expand All @@ -29,6 +29,13 @@ func getProvider(m string) (string, error) {
return "", errors.New("could not get provider")
}

func getEmbeddingProvider(m string) (string, error) {
if m == "embedding-2" {
return "chatglm", nil
}
return "openai", nil
}

func internalServerError(c *gin.Context, err error) {
c.JSON(500, gin.H{
"error": err.Error(),
Expand Down Expand Up @@ -95,7 +102,7 @@ func RegisterRoute(r *gin.Engine) {
if autoErr(c, c.BindJSON(&req)) {
return
}
provider, err := getProvider(req.Model)
provider, err := getEmbeddingProvider(req.Model)
if err != nil {
internalServerError(c, err)
return
Expand All @@ -115,27 +122,26 @@ func RegisterRoute(r *gin.Engine) {
internalServerError(c, err)
return
}
ores := map[string]any{
"object": "list",
"data": []openai.Embedding{
oores := openai.EmbeddingResponse{
Object: "list",
Data: []openai.Embedding{
{
Object: "embedding",
Index: 0,
Embedding: res.Vectors,
},
},
"model": req.Model,
"usage": map[string]any{
"prompt_tokens": res.Usage.PromptToken,
"total_tokens": res.Usage.TotalToken,
Model: openai.EmbeddingModel(req.Model),
Usage: openai.Usage{
PromptTokens: int(res.Usage.PromptToken),
TotalTokens: int(res.Usage.TotalToken),
},
}
jres, err := json.Marshal(ores)
if err != nil {
internalServerError(c, err)
return
}
c.JSON(200, jres)
c.JSON(200, oores)
})

}
Expand Down
2 changes: 1 addition & 1 deletion relay/relay_openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package relay
import (
"context"

"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
)

// TODO: read max_tokens, n, stop, frequency_penalty, presence_penalty from meta
Expand Down
2 changes: 1 addition & 1 deletion relay/reqTransformer/BaiChuan.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package reqTransformer

import (
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/provider/Baichuan"
)
Expand Down
2 changes: 1 addition & 1 deletion relay/reqTransformer/ChatGLM.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package reqTransformer

import (
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/provider/ChatGLM"
)
Expand Down
4 changes: 2 additions & 2 deletions relay/reqTransformer/ChatGPT.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package reqTransformer
import (
"log"

"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
)

Expand Down Expand Up @@ -108,7 +108,7 @@ func ChatGPTGrpcChatCompletionReq(rs *model.LLMRequestSchema) openai.ChatComplet
for i, f := range info.Functions {
tools[i] = openai.Tool{
Type: "function",
Function: openai.FunctionDefinition{
Function: &openai.FunctionDefinition{
Name: f.Name,
Description: f.Description,
Parameters: map[string]any{
Expand Down
2 changes: 1 addition & 1 deletion relay/reqTransformer/Gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package reqTransformer

import (
"github.com/Limit-LAB/go-gemini/models"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
)

Expand Down
2 changes: 1 addition & 1 deletion relay/respTransformer/ChatGPT_Grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/utils"
"google.golang.org/grpc/codes"
Expand Down
4 changes: 2 additions & 2 deletions tests_grpc/openai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/grpcServer"
"go.limit.dev/unollm/model"
"go.limit.dev/unollm/utils"
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestOpenAIEmbedding(t *testing.T) {
res, err := mockServer.EmbeddingRequestLLM(context.Background(), &model.EmbeddingRequest{
EmbeddingRequestInfo: &model.EmbeddingRequestInfo{
LlmApiType: grpcServer.OPENAI_LLM_API,
Model: openai.AdaEmbeddingV2.String(),
Model: string(openai.AdaEmbeddingV2),
Url: "https://api.openai-sb.com/v1",
Token: OPENAIApiKey,
},
Expand Down
4 changes: 2 additions & 2 deletions tests_http/baichuan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/provider/Baichuan"
tests_http "go.limit.dev/unollm/tests_http"
)
Expand Down Expand Up @@ -86,7 +86,7 @@ func GinTestBaichuanFunctionCalling(t *testing.T) {
Tools: []openai.Tool{
{
Type: openai.ToolType("function"),
Function: openai.FunctionDefinition{
Function: &openai.FunctionDefinition{
Name: "get_weather",
Description: "Get the weather of a location",
Parameters: map[string]any{
Expand Down
19 changes: 17 additions & 2 deletions tests_http/chatglm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/provider/ChatGLM"
tests_http "go.limit.dev/unollm/tests_http"
)
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestChatGLMFunctionCalling(t *testing.T) {
Tools: []openai.Tool{
{
Type: openai.ToolType("function"),
Function: openai.FunctionDefinition{
Function: &openai.FunctionDefinition{
Name: "get_weather",
Description: "Get the weather of a location",
Parameters: map[string]any{
Expand Down Expand Up @@ -123,3 +123,18 @@ func TestChatGLMFunctionCalling(t *testing.T) {
log.Printf("%#v\n", cv.Choices[0].Delta.ToolCalls)
}
}

func TestChatGLMEmbedding(t *testing.T) {
godotenv.Load("../.env")

client := tests_http.GetClient(os.Getenv("TEST_ZHIPUAI_API"))
resp, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{
Input: "我阐述你的梦",
Model: "embedding-2",
EncodingFormat: "float",
})
if err != nil {
log.Panic(err)
}
log.Printf("%#v\n", resp)
}
2 changes: 1 addition & 1 deletion tests_http/test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
"go.limit.dev/unollm/grpcServer"
)

Expand Down
2 changes: 1 addition & 1 deletion utils/openai_token_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"

"github.com/pkoukk/tiktoken-go"
"github.com/sashabaranov/go-openai"
openai "github.com/sashabaranov/go-openai"
)

var tkm *tiktoken.Tiktoken
Expand Down

0 comments on commit 8dd91f1

Please sign in to comment.