Feat: llm call retries and model switch
This commit is contained in:
		| @@ -86,12 +86,6 @@ func (p *lcpRespParser) ParseBytes(body []byte) (map[string]any, error) { | ||||
| 		p.log.Error("failed to unmarshal", "error", err) | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	// if len(resp.Choices) == 0 { | ||||
| 	// 	p.log.Error("empty choices", "resp", resp) | ||||
| 	// 	err := errors.New("empty choices in resp") | ||||
| 	// 	return nil, err | ||||
| 	// } | ||||
| 	// text := resp.Choices[0].Message.Content | ||||
| 	text := resp.Content | ||||
| 	li := strings.Index(text, "{") | ||||
| 	ri := strings.LastIndex(text, "}") | ||||
| @@ -123,11 +117,15 @@ func (p *lcpRespParser) MakePayload(prompt string) io.Reader { | ||||
| } | ||||
|  | ||||
| type openRouterParser struct { | ||||
| 	log *slog.Logger | ||||
| 	log        *slog.Logger | ||||
| 	modelIndex uint32 | ||||
| } | ||||
|  | ||||
| func NewOpenRouterParser(log *slog.Logger) *openRouterParser { | ||||
| 	return &openRouterParser{log: log} | ||||
| 	return &openRouterParser{ | ||||
| 		log:        log, | ||||
| 		modelIndex: 0, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (p *openRouterParser) ParseBytes(body []byte) (map[string]any, error) { | ||||
| @@ -160,17 +158,28 @@ func (p *openRouterParser) ParseBytes(body []byte) (map[string]any, error) { | ||||
| } | ||||
|  | ||||
| func (p *openRouterParser) MakePayload(prompt string) io.Reader { | ||||
| 	// "model": "deepseek/deepseek-chat-v3-0324:free", | ||||
| 	// TODO: set list of models an option to pick on the frontend | ||||
| 	// Models to rotate through | ||||
| 	models := []string{ | ||||
| 		"google/gemini-2.0-flash-exp:free", | ||||
| 		"deepseek/deepseek-chat-v3-0324:free", | ||||
| 		"mistralai/mistral-small-3.2-24b-instruct:free", | ||||
| 		"qwen/qwen3-14b:free", | ||||
| 		"deepseek/deepseek-r1:free", | ||||
| 		"google/gemma-3-27b-it:free", | ||||
| 		"meta-llama/llama-3.3-70b-instruct:free", | ||||
| 	} | ||||
| 	// Get next model index using atomic addition for thread safety | ||||
| 	p.modelIndex++ | ||||
| 	model := models[int(p.modelIndex)%len(models)] | ||||
| 	strPayload := fmt.Sprintf(`{ | ||||
| 	"model": "google/gemini-2.0-flash-exp:free", | ||||
| 	"model": "%s", | ||||
| 	"messages": [ | ||||
| 		{ | ||||
| 		"role": "user", | ||||
| 		"content": "%s" | ||||
| 		} | ||||
| 	] | ||||
| 	}`, prompt) | ||||
| 	p.log.Debug("made openrouter payload", "payload", strPayload) | ||||
| 	}`, model, prompt) | ||||
| 	p.log.Debug("made openrouter payload", "model", model, "payload", strPayload) | ||||
| 	return strings.NewReader(strPayload) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder