Enha: model name to config
This commit is contained in:
51
parser.go
51
parser.go
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"grailbench/config"
|
||||
"grailbench/models"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -18,10 +19,11 @@ type RespParser interface {
|
||||
// DeepSeekParser: deepseek implementation of RespParser
|
||||
type deepSeekParser struct {
|
||||
log *slog.Logger
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func NewDeepSeekParser(log *slog.Logger) *deepSeekParser {
|
||||
return &deepSeekParser{log: log}
|
||||
func NewDeepSeekParser(log *slog.Logger, cfg *config.Config) *deepSeekParser {
|
||||
return &deepSeekParser{log: log, cfg: cfg}
|
||||
}
|
||||
|
||||
func (p *deepSeekParser) ParseBytes(body []byte) (string, error) {
|
||||
@@ -36,10 +38,10 @@ func (p *deepSeekParser) ParseBytes(body []byte) (string, error) {
|
||||
err := errors.New("empty choices in dsResp")
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
// Check if the response contains tool calls
|
||||
choice := dsResp.Choices[0]
|
||||
|
||||
|
||||
// Handle response with message field (OpenAI format)
|
||||
if choice.Message.Role != "" {
|
||||
if len(choice.Message.ToolCalls) > 0 {
|
||||
@@ -51,7 +53,7 @@ func (p *deepSeekParser) ParseBytes(body []byte) (string, error) {
|
||||
// Regular text response
|
||||
return choice.Message.Content, nil
|
||||
}
|
||||
|
||||
|
||||
// Handle response with text field (legacy format)
|
||||
return choice.Text, nil
|
||||
}
|
||||
@@ -101,10 +103,11 @@ func (p *deepSeekParser) MakePayload(prompt string) io.Reader {
|
||||
// llama.cpp implementation of RespParser
|
||||
type lcpRespParser struct {
|
||||
log *slog.Logger
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func NewLCPRespParser(log *slog.Logger) *lcpRespParser {
|
||||
return &lcpRespParser{log: log}
|
||||
func NewLCPRespParser(log *slog.Logger, cfg *config.Config) *lcpRespParser {
|
||||
return &lcpRespParser{log: log, cfg: cfg}
|
||||
}
|
||||
|
||||
func (p *lcpRespParser) ParseBytes(body []byte) (string, error) {
|
||||
@@ -150,17 +153,19 @@ func (p *lcpRespParser) MakePayload(prompt string) io.Reader {
|
||||
}
|
||||
|
||||
type openRouterParser struct {
|
||||
log *slog.Logger
|
||||
modelIndex uint32
|
||||
useChatAPI bool
|
||||
log *slog.Logger
|
||||
cfg *config.Config
|
||||
modelIndex uint32
|
||||
useChatAPI bool
|
||||
supportsTools bool
|
||||
}
|
||||
|
||||
func NewOpenRouterParser(log *slog.Logger) *openRouterParser {
|
||||
func NewOpenRouterParser(log *slog.Logger, cfg *config.Config) *openRouterParser {
|
||||
return &openRouterParser{
|
||||
log: log,
|
||||
modelIndex: 0,
|
||||
useChatAPI: false, // Default to completion API which is more widely supported
|
||||
log: log,
|
||||
cfg: cfg,
|
||||
modelIndex: 0,
|
||||
useChatAPI: false, // Default to completion API which is more widely supported
|
||||
supportsTools: false, // Don't assume tool support
|
||||
}
|
||||
}
|
||||
@@ -178,9 +183,9 @@ func (p *openRouterParser) ParseBytes(body []byte) (string, error) {
|
||||
err := errors.New("empty choices in openrouter chat response")
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
choice := resp.Choices[0]
|
||||
|
||||
|
||||
// Check if the response contains tool calls
|
||||
if len(choice.Message.ToolCalls) > 0 {
|
||||
// Handle tool call response
|
||||
@@ -188,11 +193,11 @@ func (p *openRouterParser) ParseBytes(body []byte) (string, error) {
|
||||
// Return a special marker indicating tool usage
|
||||
return fmt.Sprintf("[TOOL_CALL:%s]", toolCall.Function.Name), nil
|
||||
}
|
||||
|
||||
|
||||
// Regular text response
|
||||
return choice.Message.Content, nil
|
||||
}
|
||||
|
||||
|
||||
// If using completion API, parse as text completion response (no tool calls)
|
||||
resp := models.ORCompletionResp{}
|
||||
if err := json.Unmarshal(body, &resp); err != nil {
|
||||
@@ -204,7 +209,7 @@ func (p *openRouterParser) ParseBytes(body []byte) (string, error) {
|
||||
err := errors.New("empty choices in openrouter completion response")
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
// Return the text content
|
||||
return resp.Choices[0].Text, nil
|
||||
}
|
||||
@@ -213,10 +218,10 @@ func (p *openRouterParser) MakePayload(prompt string) io.Reader {
|
||||
if p.useChatAPI {
|
||||
// Use chat completions API with messages format (supports tool calls)
|
||||
payload := struct {
|
||||
Model string `json:"model"`
|
||||
Model string `json:"model"`
|
||||
Messages []models.RoleMsg `json:"messages"`
|
||||
}{
|
||||
Model: "openai/gpt-4o-mini",
|
||||
Model: p.cfg.ModelName,
|
||||
Messages: []models.RoleMsg{
|
||||
{Role: "user", Content: prompt},
|
||||
},
|
||||
@@ -230,13 +235,13 @@ func (p *openRouterParser) MakePayload(prompt string) io.Reader {
|
||||
p.log.Debug("made openrouter chat payload", "payload", string(b))
|
||||
return bytes.NewReader(b)
|
||||
}
|
||||
|
||||
|
||||
// Use completions API with prompt format (no tool calls)
|
||||
payload := struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
}{
|
||||
Model: "openai/gpt-4o-mini",
|
||||
Model: p.cfg.ModelName,
|
||||
Prompt: prompt,
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user