Refactor Google LLM API to use chat session interface.

Replace message handling with a chat session model, aligning the logic with new API requirements. Adjust functions to properly build chat history and send messages via chat sessions, improving compatibility and extensibility.
This commit is contained in:
Steve Dudenhoeffer 2025-01-22 22:07:20 -05:00
parent e7b7aab62e
commit 388a44fa79

View File

@ -20,34 +20,29 @@ func (g google) ModelVersion(modelVersion string) (ChatCompletion, error) {
return g, nil return g, nil
} }
func (g google) requestToGoogleRequest(in Request, model *genai.GenerativeModel) []genai.Part { func (g google) requestToChatHistory(in Request, model *genai.GenerativeModel) (*genai.ChatSession, []genai.Part) {
cs := model.StartChat()
if in.Temperature != nil { for i, c := range in.Messages {
model.GenerationConfig.Temperature = in.Temperature content := genai.NewUserContent(genai.Text(c.Text))
switch c.Role {
case RoleAssistant, RoleSystem:
content.Role = "model"
case RoleUser:
content.Role = "user"
} }
res := []genai.Part{} // if this is the last message, we want to add to history, we want it to be the parts
if i == len(in.Messages)-1 {
for _, c := range in.Messages { return cs, content.Parts
res = append(res, genai.Text(c.Text))
} }
if in.Toolbox != nil { cs.History = append(cs.History, content)
for _, tool := range in.Toolbox.funcs {
panic("google ToolBox is todo" + tool.Name)
/*
t := genai.Tool{}
t.FunctionDeclarations = append(t.FunctionDeclarations, &genai.FunctionDeclaration{
Name: tool.Name,
Description: tool.Description,
Parameters: nil, //tool.Parameters,
})
*/
}
} }
return res return cs, nil
} }
func (g google) responseToLLMResponse(in *genai.GenerateContentResponse) (Response, error) { func (g google) responseToLLMResponse(in *genai.GenerateContentResponse) (Response, error) {
@ -104,9 +99,13 @@ func (g google) ChatComplete(ctx context.Context, req Request) (Response, error)
model := cl.GenerativeModel(g.model) model := cl.GenerativeModel(g.model)
parts := g.requestToGoogleRequest(req, model) cs, parts := g.requestToChatHistory(req, model)
resp, err := model.GenerateContent(ctx, parts...) resp, err := cs.SendMessage(ctx, parts...)
//parts := g.requestToGoogleRequest(req, model)
//resp, err := model.GenerateContent(ctx, parts...)
if err != nil { if err != nil {
return Response{}, fmt.Errorf("error generating content: %w", err) return Response{}, fmt.Errorf("error generating content: %w", err)