Skip to content

Instantly share code, notes, and snippets.

View wangjohn's full-sized avatar

John J. Wang wangjohn

View GitHub Profile
type ResponseCleaner interface {
Clean(context.Context, string) (string, []ResponseDetails, error)
}
type ResponseDetails struct {
DetailType string `json:"detail_type"`
Content interface{} `json:"content"`
}
type CitedSourceCleaner struct{}
func (c CitedSourceCleaner) Clean(ctx context.Context, message string) (string, []ResponseDetails, error) {
sourceRegex := regexp.MustCompile(`\[(Source|Ref):\s*([^\]]+)\]`)
var citations []ResponseDetails
matches := sourceRegex.FindAllStringSubmatch(message, -1)
for i, match := range matches {
citations = append(citations, ResponseDetails{
DetailType: "citation",
@wangjohn
wangjohn / model_categories.go
Created June 13, 2025 02:25
LLM fallback article
type ModelCategory string
const (
Fast ModelCategory = "fast"
Smart ModelCategory = "smart"
Reasoning ModelCategory = "reasoning"
)
func GetModelForCategory(category ModelCategory, platform ModelPlatform) ModelType {
switch platform {
@wangjohn
wangjohn / global_fallback_order.go
Created June 13, 2025 02:26
LLM fallback article
var GlobalFallbackOrder = []ModelPlatform{
ModelPlatformOpenAI, // Primary choice
ModelPlatformAnthropic, // Secondary
ModelPlatformGemini, // Tertiary
}
@wangjohn
wangjohn / create_completion.go
Created June 13, 2025 02:27
LLM fallback article
func (agent *LLMAgent) CreateCompletion(ctx context.Context, request *CompletionRequest) (*Response, error) {
// Build list of primary model + fallbacks for same category
modelsToTry := []ModelType{agent.primaryModel}
for _, platform := range GlobalFallbackOrder {
if platform != agent.primaryPlatform {
fallbackModel := GetModelForCategory(agent.category, platform)
modelsToTry = append(modelsToTry, fallbackModel)
}
}
@wangjohn
wangjohn / stream_completion.go
Created June 13, 2025 02:28
LLM fallback article
func (agent *LLMAgent) StreamCompletion(ctx context.Context, request *CompletionRequest, tokens chan<- Token) error {
modelsToTry := agent.getModelsToTry()
for i, model := range modelsToTry {
hasReceivedFirstToken, err := agent.tryStreamSingleModel(ctx, request, model, tokens, agent.getModelTimeout())
if err == nil {
return nil // Success
}