MCPcopy Create free account
hub / github.com/grapeot/devin.cursorrules / query_llm

Function query_llm

tools/llm_api.py:124–242  ·  view source on GitHub ↗

Query an LLM with a prompt and optional image attachment. Args: prompt (str): The text prompt to send client: The LLM client instance model (str, optional): The model to use provider (str): The API provider to use image_path (str, optional): Path

(prompt: str, client=None, model=None, provider="openai", image_path: Optional[str] = None)

Source from the content-addressed store, hash-verified

122 raise ValueError(f"Unsupported provider: {provider}")
123
124def query_llm(prompt: str, client=None, model=None, provider="openai", image_path: Optional[str] = None) -> Optional[str]:
125 """
126 Query an LLM with a prompt and optional image attachment.
127
128 Args:
129 prompt (str): The text prompt to send
130 client: The LLM client instance
131 model (str, optional): The model to use
132 provider (str): The API provider to use
133 image_path (str, optional): Path to an image file to attach
134
135 Returns:
136 Optional[str]: The LLM's response or None if there was an error
137 """
138 if client is None:
139 client = create_llm_client(provider)
140
141 try:
142 # Set default model
143 if model is None:
144 if provider == "openai":
145 model = os.getenv('OPENAI_MODEL_DEPLOYMENT', 'gpt-4o')
146 elif provider == "azure":
147 model = os.getenv('AZURE_OPENAI_MODEL_DEPLOYMENT', 'gpt-4o-ms') # Get from env with fallback
148 elif provider == "deepseek":
149 model = "deepseek-chat"
150 elif provider == "siliconflow":
151 model = "deepseek-ai/DeepSeek-R1"
152 elif provider == "anthropic":
153 model = "claude-3-7-sonnet-20250219"
154 elif provider == "gemini":
155 model = "gemini-2.0-flash-exp"
156 elif provider == "local":
157 model = "Qwen/Qwen2.5-32B-Instruct-AWQ"
158
159 if provider in ["openai", "local", "deepseek", "azure", "siliconflow"]:
160 messages = [{"role": "user", "content": []}]
161
162 # Add text content
163 messages[0]["content"].append({
164 "type": "text",
165 "text": prompt
166 })
167
168 # Add image content if provided
169 if image_path:
170 if provider == "openai":
171 encoded_image, mime_type = encode_image_file(image_path)
172 messages[0]["content"] = [
173 {"type": "text", "text": prompt},
174 {"type": "image_url", "image_url": {"url": f"data:{mime_type};base64,{encoded_image}"}}
175 ]
176
177 kwargs = {
178 "model": model,
179 "messages": messages,
180 "temperature": 0.7,
181 }

Callers 15

is_llm_configuredFunction · 0.90
test_query_openaiMethod · 0.90
test_query_azureMethod · 0.90
test_query_deepseekMethod · 0.90
test_query_anthropicMethod · 0.90
test_query_geminiMethod · 0.90
test_query_localMethod · 0.90
test_query_o1_modelMethod · 0.90

Calls 2

create_llm_clientFunction · 0.85
encode_image_fileFunction · 0.85

Tested by 14

is_llm_configuredFunction · 0.72
test_query_openaiMethod · 0.72
test_query_azureMethod · 0.72
test_query_deepseekMethod · 0.72
test_query_anthropicMethod · 0.72
test_query_geminiMethod · 0.72
test_query_localMethod · 0.72
test_query_o1_modelMethod · 0.72