Skip to content

Instantly share code, notes, and snippets.

View wangjohn's full-sized avatar

John J. Wang wangjohn

View GitHub Profile
@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
}
@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 / 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 / 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 {
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",
type ResponseCleaner interface {
Clean(context.Context, string) (string, []ResponseDetails, error)
}
type ResponseDetails struct {
DetailType string `json:"detail_type"`
Content interface{} `json:"content"`
}
import (
"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
)
type SupportResponse struct {
Answer string `json:"answer"`
RelatedDocs []string `json:"related_docs"`
}
func ParallelSearch(query string) []SearchResult {
ctx, cancel := context.WithTimeout(context.Background(), 750*time.Millisecond)
defer cancel()
resultsChan := make(chan []SearchResult, len(backends))
var wg sync.WaitGroup
for _, backend := range backends {
wg.Add(1)
go func(backend func(string) ([]SearchResult, error)) {
@wangjohn
wangjohn / Example prompt for Claude 3.5 Sonnet
Last active October 28, 2024 18:35
Example of generating typescript tests using Claude 3.5 Sonnet
Help me write a comprehensive set of unit tests in Typescript for the following function:
<function_to_test>
function romanToInt(roman: string): number {
const romanNumerals: { [key: string]: number } = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
{{#bs-page-heading verbose=true title="Welcome back!" subtitle=(
format-message (intl-get 'messages.dashboardOverview.subtitle')
percentChange=(format-number averageChargePercentChange style='percent')
previous=(format-currency previousAverageCharge currency=currency)
current=(format-currency currentAverageCharge currency=currency)
sign=(compute-sign averageChargePercentChange bufferedFractionDigits=2)
timespan=(format-message (intl-get 'messages.helpers.timeElapsed')
unitCount=(time-elapsed overviewTimespan unit='months') unit='months')
)
}}