in openai/chat.go [99:137]
func toOpenAIResponse(from *genai.GenerateContentResponse, object, model string) (to ChatCompletionResponse) {
to.Object = object
to.Created = time.Now().Unix()
to.Model = model
if from.UsageMetadata != nil {
to.Usage = Usage{
PromptTokens: from.UsageMetadata.PromptTokenCount,
CompletionTokens: from.UsageMetadata.CandidatesTokenCount,
TotalTokens: from.UsageMetadata.TotalTokenCount,
}
}
to.Choices = make([]ChatCompletionChoice, 0, len(from.Candidates))
for i, c := range from.Candidates {
var builder strings.Builder
for _, p := range c.Content.Parts {
content, ok := p.(genai.Text)
if !ok {
log.Printf("failed to process content part; type = %v", reflect.TypeOf(p))
continue
}
builder.WriteString(string(content))
}
choice := ChatCompletionChoice{
Index: i,
Message: ChatMessage{
Role: c.Content.Role,
Content: builder.String(),
},
}
finishReason := toGeminiFinishReason(c.FinishReason)
if finishReason != "" {
choice.FinishReason = finishReason
}
to.Choices = append(to.Choices, choice)
}
return to
}