package your_integration
import (
"github.com/maximhq/bifrost/core/schemas"
"github.com/maximhq/bifrost/transports/bifrost-http/integrations"
)
// YourChatRequest represents the incoming request format
type YourChatRequest struct {
Model string `json:"model"`
Messages []YourMessage `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
Tools []YourTool `json:"tools,omitempty"`
// Add fields specific to your service
}
// YourMessage represents a chat message in your service format
type YourMessage struct {
Role string `json:"role"`
Content interface{} `json:"content"`
}
// YourChatResponse represents the response format
type YourChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Model string `json:"model"`
Choices []YourChoice `json:"choices"`
Usage YourUsage `json:"usage"`
}
// ConvertToBifrostRequest converts your service format to Bifrost format
func (r *YourChatRequest) ConvertToBifrostRequest() *schemas.BifrostRequest {
// Enable multi-provider support with ParseModelString
// This allows users to specify "provider/model" (e.g., "anthropic/claude-3-sonnet")
// or just "model" (uses your integration's default provider)
provider, modelName := integrations.ParseModelString(r.Model, schemas.YourDefaultProvider)
// Convert messages
bifrostMessages := make([]schemas.ModelChatMessage, len(r.Messages))
for i, msg := range r.Messages {
bifrostMessages[i] = schemas.ModelChatMessage{
Role: schemas.ModelChatMessageRole(msg.Role),
Content: convertContentToBifrost(msg.Content),
}
}
// Convert tools if present
var bifrostTools []schemas.ChatCompletionTool
if len(r.Tools) > 0 {
bifrostTools = convertToolsToBifrost(r.Tools)
}
return &schemas.BifrostRequest{
Model: modelName, // Clean model name without provider prefix
Provider: provider, // Extracted or default provider
MaxTokens: &r.MaxTokens,
Temperature: r.Temperature,
Input: schemas.BifrostInput{
ChatCompletionInput: &bifrostMessages,
},
Tools: bifrostTools,
}
}
// ConvertBifrostToYourResponse converts Bifrost response to your service format
func ConvertBifrostToYourResponse(resp *schemas.BifrostResponse) *YourChatResponse {
if resp.ChatCompletionOutput == nil {
return &YourChatResponse{}
}
choices := make([]YourChoice, len(resp.ChatCompletionOutput.Choices))
for i, choice := range resp.ChatCompletionOutput.Choices {
choices[i] = YourChoice{
Index: i,
Message: YourMessage{
Role: string(choice.Message.Role),
Content: convertContentFromBifrost(choice.Message.Content),
},
FinishReason: string(choice.FinishReason),
}
}
return &YourChatResponse{
ID: resp.ID,
Object: "chat.completion",
Model: resp.Model,
Choices: choices,
Usage: YourUsage{
PromptTokens: resp.ChatCompletionOutput.Usage.PromptTokens,
CompletionTokens: resp.ChatCompletionOutput.Usage.CompletionTokens,
TotalTokens: resp.ChatCompletionOutput.Usage.TotalTokens,
},
}
}
// Helper functions for content conversion
func convertContentToBifrost(content interface{}) schemas.ModelChatMessageContent {
// Implementation depends on your service's content format
// Handle text, images, tool calls, etc.
}
func convertContentFromBifrost(content schemas.ModelChatMessageContent) interface{} {
// Convert Bifrost content back to your service format
}
func convertToolsToBifrost(tools []YourTool) []schemas.ChatCompletionTool {
// Convert tools to Bifrost format
}