Skip to content

Commit

Permalink
fix: missing fields and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
ho-229 committed Apr 3, 2024
1 parent bcd700a commit c90ad15
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 43 deletions.
7 changes: 3 additions & 4 deletions grpcServer/baichuan.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package grpcServer

import (
"log"

"go.limit.dev/unollm/model"
"go.limit.dev/unollm/provider/Baichuan"
"go.limit.dev/unollm/relay"
"go.limit.dev/unollm/relay/reqTransformer"
"go.limit.dev/unollm/relay/respTransformer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
)

func BaichuanChatCompletion(cli *Baichuan.Client, rs *model.LLMRequestSchema) (*model.LLMResponseSchema, error) {
Expand All @@ -33,9 +34,7 @@ func BaichuanChatCompletionStream(cli *Baichuan.Client, rs *model.LLMRequestSche
res, err := relay.BaiChuanChatCompletionStreamingRequest(cli, req)

if err != nil {
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
return status.Errorf(codes.Internal, err.Error())
}

return respTransformer.BaiChuanToGrpcStream(res, sv)
Expand Down
6 changes: 0 additions & 6 deletions grpcServer/relay_gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,7 @@ func RegisterRoute(r *gin.Engine) {
TotalTokens: int(res.Usage.TotalToken),
},
}
if err != nil {
internalServerError(c, err)
return
}
c.JSON(200, oores)
})

}

}
24 changes: 11 additions & 13 deletions relay/respTransformer/ChatGLM_Grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ func ChatGLMToGrpcCompletion(res ChatGLM.ChatCompletionResponse) (*model.LLMResp
}

toolCalls := make([]*model.ToolCall, len(res.Choices[0].Message.ToolCalls))
for i, _ := range res.Choices[0].Message.ToolCalls {
toolcall := model.ToolCall{
Id: res.Choices[0].Message.ToolCalls[i].Id,
Name: res.Choices[0].Message.ToolCalls[i].Function.Name,
Arguments: res.Choices[0].Message.ToolCalls[i].Function.Arguments,
for i, toolCall := range res.Choices[0].Message.ToolCalls {
toolCalls[i] = &model.ToolCall{
Id: toolCall.Id,
Name: toolCall.Function.Name,
Arguments: toolCall.Function.Arguments,
}
toolCalls[i] = &toolcall
}
retResp := model.LLMResponseSchema{
Message: &retMessage,
Expand All @@ -41,20 +40,19 @@ func ChatGLMToGrpcStream(_r *ChatGLM.ChatCompletionStreamingResponse, sv model.U
select {
case chunk := <-llm:
toolCalls := make([]*model.ToolCall, len(chunk.Choices[0].Delta.ToolCalls))
for i, _ := range chunk.Choices[0].Delta.ToolCalls {
toolcall := model.ToolCall{
Id: chunk.Choices[0].Delta.ToolCalls[i].Id,
Name: chunk.Choices[0].Delta.ToolCalls[i].Function.Name,
Arguments: chunk.Choices[0].Delta.ToolCalls[i].Function.Arguments,
for i, toolCall := range chunk.Choices[0].Delta.ToolCalls {
toolCalls[i] = &model.ToolCall{
Id: toolCall.Id,
Name: toolCall.Function.Name,
Arguments: toolCall.Function.Arguments,
}
toolCalls[i] = &toolcall
}

resp := model.PartialLLMResponse{
ToolCalls: toolCalls,
Response: &model.PartialLLMResponse_Content{
Content: chunk.Choices[0].Delta.Content,
},
ToolCalls: toolCalls,
}
sv.Send(&resp)
case res := <-result:
Expand Down
37 changes: 18 additions & 19 deletions relay/respTransformer/ChatGPT_Grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ func ChatGPTToGrpcCompletion(resp openai.ChatCompletionResponse) (*model.LLMResp
CompletionToken: int64(resp.Usage.CompletionTokens),
}
toolCalls := make([]*model.ToolCall, len(resp.Choices[0].Message.ToolCalls))
for i, _ := range resp.Choices[0].Message.ToolCalls {
toolcall := model.ToolCall{
Id: resp.Choices[0].Message.ToolCalls[i].ID,
Name: resp.Choices[0].Message.ToolCalls[i].Function.Name,
Arguments: resp.Choices[0].Message.ToolCalls[i].Function.Arguments,
for i, toolCall := range resp.Choices[0].Message.ToolCalls {
toolCalls[i] = &model.ToolCall{
Id: toolCall.ID,
Name: toolCall.Function.Name,
Arguments: toolCall.Function.Arguments,
}
toolCalls[i] = &toolcall
}
retResp := model.LLMResponseSchema{
Message: &retMessage,
Expand Down Expand Up @@ -76,18 +75,18 @@ func ChatGPTToGrpcStream(promptTokens int, resp *openai.ChatCompletionStream, sv
i++

toolCalls := make([]*model.ToolCall, len(response.Choices[0].Delta.ToolCalls))
for i, _ := range response.Choices[0].Delta.ToolCalls {
toolcall := model.ToolCall{
Id: response.Choices[0].Delta.ToolCalls[i].ID,
Name: response.Choices[0].Delta.ToolCalls[i].Function.Name,
Arguments: response.Choices[0].Delta.ToolCalls[i].Function.Arguments,
for i, toolCall := range response.Choices[0].Delta.ToolCalls {
toolCalls[i] = &model.ToolCall{
Id: toolCall.ID,
Name: toolCall.Function.Name,
Arguments: toolCall.Function.Arguments,
}
toolCalls[i] = &toolcall
}
pr := model.PartialLLMResponse{
Response: &model.PartialLLMResponse_Content{
Content: message,
},
ToolCalls: toolCalls,
}
sv.Send(&pr)
}
Expand Down Expand Up @@ -143,14 +142,14 @@ func GrpcStreamToChatGPT(c *gin.Context, model string, sv chan *model.PartialLLM
c.Stream(func(w io.Writer) bool {
pr := <-sv
if pr.LlmTokenCount == nil {
toolcalls := make([]openai.ToolCall, len(pr.GetToolCalls()))
for i, _ := range pr.GetToolCalls() {
toolcalls[i] = openai.ToolCall{
ID: pr.GetToolCalls()[i].Id,
toolCalls := make([]openai.ToolCall, len(pr.GetToolCalls()))
for i, toolCall := range pr.ToolCalls {
toolCalls[i] = openai.ToolCall{
ID: toolCall.Id,
Type: openai.ToolType("function"),
Function: openai.FunctionCall{
Name: pr.GetToolCalls()[i].Name,
Arguments: pr.GetToolCalls()[i].Arguments,
Name: toolCall.Name,
Arguments: toolCall.Arguments,
},
}
}
Expand All @@ -164,7 +163,7 @@ func GrpcStreamToChatGPT(c *gin.Context, model string, sv chan *model.PartialLLM
Delta: openai.ChatCompletionStreamChoiceDelta{
Role: "assistant",
Content: pr.GetContent(),
ToolCalls: toolcalls,
ToolCalls: toolCalls,
},
},
},
Expand Down
6 changes: 5 additions & 1 deletion relay/respTransformer/Gemini_Grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package respTransformer
import (
"errors"
"fmt"
"io"

"github.com/Limit-LAB/go-gemini"
"github.com/Limit-LAB/go-gemini/models"
"go.limit.dev/unollm/model"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
)

func GeminiToGrpcCompletion(resp models.GenerateContentResponse) (*model.LLMResponseSchema, error) {
Expand Down Expand Up @@ -78,6 +79,9 @@ func GeminiToGrpcStream(resp *gemini.GenerateContentStreamer, sv model.UnoLLMv1_
}

_, msg, err := getGeminiContent(response)
if err != nil {
return err
}
pr := model.PartialLLMResponse{
Response: &model.PartialLLMResponse_Content{
Content: msg,
Expand Down

0 comments on commit c90ad15

Please sign in to comment.