MCPcopy Index your code
hub / github.com/FSoft-AI4Code/CodeWiki / call_llm

Function call_llm

codewiki/src/be/llm_services.py:148–212  ·  view source on GitHub ↗

Call LLM with the given prompt. Supports openai-compatible, anthropic, and bedrock providers. For bedrock/anthropic, uses litellm to translate the API call. Args: prompt: The prompt to send config: Configuration containing LLM settings model: Model name (de

(
    prompt: str,
    config: Config,
    model: str = None,
    temperature: float = 0.0
)

Source from the content-addressed store, hash-verified

146
147
148def call_llm(
149 prompt: str,
150 config: Config,
151 model: str = None,
152 temperature: float = 0.0
153) -> str:
154 """
155 Call LLM with the given prompt.
156
157 Supports openai-compatible, anthropic, and bedrock providers.
158 For bedrock/anthropic, uses litellm to translate the API call.
159
160 Args:
161 prompt: The prompt to send
162 config: Configuration containing LLM settings
163 model: Model name (defaults to config.main_model)
164 temperature: Temperature setting
165
166 Returns:
167 LLM response text
168 """
169 if model is None:
170 model = config.main_model
171
172 provider = getattr(config, "provider", "openai-compatible")
173
174 if provider in ("bedrock", "anthropic"):
175 return _call_llm_via_litellm(prompt, config, model, temperature)
176
177 if provider == "azure-openai":
178 return _call_llm_via_azure(prompt, config, model, temperature)
179
180 # Default: OpenAI-compatible
181 client = create_openai_client(config)
182
183 # Use the correct token parameter based on model/provider; if the server
184 # rejects our choice, swap to the other token kwarg and retry once.
185 use_completion_tokens = _should_use_max_completion_tokens(model, config.llm_base_url)
186 primary_key = "max_completion_tokens" if use_completion_tokens else "max_tokens"
187 fallback_key = "max_tokens" if use_completion_tokens else "max_completion_tokens"
188
189 base_kwargs = {
190 "model": model,
191 "messages": [{"role": "user", "content": prompt}],
192 "temperature": temperature,
193 }
194
195 try:
196 response = client.chat.completions.create(
197 **base_kwargs,
198 **{primary_key: config.max_tokens},
199 )
200 except BadRequestError as e:
201 if _is_unsupported_token_param_error(e, primary_key):
202 logger.info(
203 "Provider rejected %s for model %s; retrying with %s.",
204 primary_key, model, fallback_key,
205 )

Callers 2

completeMethod · 0.90
cluster_modulesFunction · 0.90

Calls 7

_call_llm_via_litellmFunction · 0.85
_call_llm_via_azureFunction · 0.85
create_openai_clientFunction · 0.85
createMethod · 0.80
infoMethod · 0.80

Tested by

no test coverage detected