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

Function chat_with_database

web/pgadmin/llm/chat.py:46–154  ·  view source on GitHub ↗

Run an LLM chat conversation with database tool access. This function handles the full conversation loop, executing any tool calls the LLM makes and continuing until a final response is generated. Args: user_message: The user's message/question sid: Server ID f

(
    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

44
45
46def chat_with_database(
47 user_message: str,
48 sid: int,
49 did: int,
50 conversation_history: Optional[list[Message]] = None,
51 system_prompt: Optional[str] = None,
52 max_tool_iterations: Optional[int] = None,
53 provider: Optional[str] = None,
54 model: Optional[str] = None
55) -> tuple[str, list[Message]]:
56 """
57 Run an LLM chat conversation with database tool access.
58
59 This function handles the full conversation loop, executing any
60 tool calls the LLM makes and continuing until a final response
61 is generated.
62
63 Args:
64 user_message: The user's message/question
65 sid: Server ID for database connection
66 did: Database ID for database connection
67 conversation_history: Optional list of previous messages
68 system_prompt: Optional custom system prompt (uses default if None)
69 max_tool_iterations: Maximum number of tool call
70 rounds. Uses preference setting if None.
71 provider: Optional LLM provider override
72 model: Optional model override
73
74 Returns:
75 Tuple of (final_response_text, updated_conversation_history)
76
77 Raises:
78 LLMClientError: If the LLM request fails
79 RuntimeError: If LLM is not available or max iterations exceeded
80 """
81 if not is_llm_available():
82 raise RuntimeError("LLM is not configured. Please configure an LLM "
83 "provider in Preferences > AI.")
84
85 client = get_llm_client(provider=provider, model=model)
86 if not client:
87 raise RuntimeError("Failed to create LLM client")
88
89 # Initialize conversation history
90 messages = list(conversation_history) if conversation_history else []
91 messages.append(Message.user(user_message))
92
93 # Use default system prompt if none provided
94 if system_prompt is None:
95 system_prompt = DEFAULT_SYSTEM_PROMPT
96
97 # Get max iterations from preferences if not specified
98 if max_tool_iterations is None:
99 max_tool_iterations = get_max_tool_iterations()
100
101 iteration = 0
102 while iteration < max_tool_iterations:
103 iteration += 1

Callers 1

single_queryFunction · 0.85

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
chatMethod · 0.45

Tested by

no test coverage detected