MCPcopy
hub / github.com/the-open-agent/openagent / QueryText

Method QueryText

model/openrouter.go:131–239  ·  view source on GitHub ↗
(question string, writer io.Writer, history []*RawMessage, prompt string, knowledgeMessages []*RawMessage, toolSession *ToolSession, lang string)

Source from the content-addressed store, hash-verified

129}
130
131func (p *OpenRouterModelProvider) QueryText(question string, writer io.Writer, history []*RawMessage, prompt string, knowledgeMessages []*RawMessage, toolSession *ToolSession, lang string) (*ModelResult, error) {
132 client := p.getProxyClientFromToken()
133
134 ctx := context.Background()
135 flusher, ok := writer.(http.Flusher)
136 if !ok {
137 return nil, fmt.Errorf(i18n.Translate(lang, "model:writer does not implement http.Flusher"))
138 }
139
140 model := p.subType
141 if model == "" {
142 model = openrouter.Gpt35Turbo
143 }
144
145 tokenCount, err := GetTokenSize(model, question)
146 if err != nil {
147 return nil, err
148 }
149
150 contextLength := getContextLength(p.subType)
151
152 if strings.HasPrefix(question, "$OpenAgentDryRun$") {
153 modelResult, err := getDefaultModelResult(model, question, "")
154 if err != nil {
155 return nil, fmt.Errorf(i18n.Translate(lang, "model:cannot calculate tokens"))
156 }
157 if contextLength > modelResult.TotalTokenCount {
158 return modelResult, nil
159 } else {
160 return nil, fmt.Errorf(i18n.Translate(lang, "model:exceed max tokens"))
161 }
162 }
163
164 maxTokens := contextLength - tokenCount
165 if maxTokens < 0 {
166 return nil, fmt.Errorf(i18n.Translate(lang, "model:The token count: [%d] exceeds the model: [%s]'s maximum token count: [%d]"), tokenCount, model, contextLength)
167 }
168
169 temperature := p.temperature
170 topP := p.topP
171
172 respStream, err := client.CreateChatCompletionStream(
173 ctx,
174 &openrouter.ChatCompletionRequest{
175 Model: p.subType,
176 Messages: []openrouter.ChatCompletionMessage{
177 {
178 Role: openrouter.ChatMessageRoleSystem,
179 Content: "You are a helpful assistant.",
180 },
181 {
182 Role: openrouter.ChatMessageRoleUser,
183 Content: question,
184 },
185 },
186 Stream: false,
187 Temperature: temperature,
188 TopP: topP,

Callers

nothing calls this directly

Calls 9

calculatePriceMethod · 0.95
TranslateFunction · 0.92
GetTokenSizeFunction · 0.85
getContextLengthFunction · 0.85
getDefaultModelResultFunction · 0.85
CloseMethod · 0.45
FlushMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected