MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / chat_with_database_stream

Function chat_with_database_stream

web/pgadmin/llm/chat.py:157–266  ·  view source on GitHub ↗

Stream an LLM chat conversation with database tool access. Like chat_with_database, but yields text chunks as the final response streams in. During tool-use iterations, no text is yielded (tools are executed silently). Yields: str: Text content chunks from the final LL

(
    user_message: str,
    sid: int,
    did: int,
    conversation_history: Optional[list[Message]] = None,
    system_prompt: Optional[str] = None,
    max_tool_iterations: Optional[int] = None,
    provider: Optional[str] = None,
    model: Optional[str] = None
)

Source from the content-addressed store, hash-verified

155
156
157def chat_with_database_stream(
158 user_message: str,
159 sid: int,
160 did: int,
161 conversation_history: Optional[list[Message]] = None,
162 system_prompt: Optional[str] = None,
163 max_tool_iterations: Optional[int] = None,
164 provider: Optional[str] = None,
165 model: Optional[str] = None
166) -> Generator[Union[str, tuple], None, None]:
167 """
168 Stream an LLM chat conversation with database tool access.
169
170 Like chat_with_database, but yields text chunks as the final
171 response streams in. During tool-use iterations, no text is
172 yielded (tools are executed silently).
173
174 Yields:
175 str: Text content chunks from the final LLM response.
176
177 The last item yielded is a 3-tuple of
178 ('complete', final_response_text, updated_conversation_history).
179
180 Raises:
181 LLMClientError: If the LLM request fails.
182 RuntimeError: If LLM is not available or max iterations exceeded.
183 """
184 if not is_llm_available():
185 raise RuntimeError("LLM is not configured. Please configure an LLM "
186 "provider in Preferences > AI.")
187
188 client = get_llm_client(provider=provider, model=model)
189 if not client:
190 raise RuntimeError("Failed to create LLM client")
191
192 messages = list(conversation_history) if conversation_history else []
193 messages.append(Message.user(user_message))
194
195 if system_prompt is None:
196 system_prompt = DEFAULT_SYSTEM_PROMPT
197
198 if max_tool_iterations is None:
199 max_tool_iterations = get_max_tool_iterations()
200
201 iteration = 0
202 while iteration < max_tool_iterations:
203 iteration += 1
204
205 # Stream the LLM response, yielding text chunks as they arrive
206 response = None
207 for item in client.chat_stream(
208 messages=messages,
209 tools=DATABASE_TOOLS,
210 system_prompt=system_prompt
211 ):
212 if isinstance(item, LLMResponse):
213 response = item
214 elif isinstance(item, str):

Callers 1

generateFunction · 0.90

Calls 10

is_llm_availableFunction · 0.90
get_llm_clientFunction · 0.90
get_max_tool_iterationsFunction · 0.90
execute_toolFunction · 0.90
appendMethod · 0.80
userMethod · 0.80
to_messageMethod · 0.80
tool_resultMethod · 0.80
extendMethod · 0.80
chat_streamMethod · 0.45

Tested by

no test coverage detected