Skip to content

Commit da5549e

Browse files
committed
Add data tool function and improve response printing
Added `make_data_tool` function which is responsible for generating function metadata for openai tools. Also, replaced ordinary `print` of response from OpenAI with `pretty_print_conversation` in `using_mindsdb_inference_with_text2sql_prompt.py` to enhance visibility of conversation.
1 parent 1b5f51d commit da5549e

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

examples/using_mindsdb_inference_with_text2sql_prompt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from openai import OpenAI
2-
from mindsdb_sdk.utils.openai import extract_sql_query, query_database, chat_completion_request
2+
from mindsdb_sdk.utils.openai import extract_sql_query, query_database, chat_completion_request, \
3+
pretty_print_conversation
34

45
import mindsdb_sdk
56
import os
@@ -90,4 +91,5 @@ def generate_user_prompt(query: str) -> dict:
9091
tool_choice=None)
9192
response = chat_completion_gpt.choices[0].message.content
9293

93-
print(response)
94+
pretty_print_conversation(messages)
95+

mindsdb_sdk/utils/openai.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,64 @@ def make_mindsdb_tool(schema: dict) -> dict:
109109
}
110110

111111

112+
def make_data_tool(
113+
model: str,
114+
data_source: str,
115+
description: str,
116+
connection_args: dict
117+
):
118+
"""
119+
tool passing connection details for datasource to litellm callback
120+
121+
:param model: model name for text to sql completion
122+
:param data_source: data source name
123+
:param description: description of the data source
124+
:param connection_args: connection arguments for the data source
125+
126+
:return: dictionary containing function metadata for openai tools
127+
"""
128+
# Convert the connection_args dictionary to a JSON object
129+
connection_args_json = json.dumps(connection_args)
130+
131+
tool_description = f"""
132+
Queries the provided data source about user data. When calling this function, ALWAYS use the following arguments:
133+
- model: {model}
134+
- connection_args: {connection_args_json}
135+
- data_source: {data_source}
136+
- description: {description}
137+
"""
138+
139+
return {
140+
"type":"function",
141+
"function":{
142+
"name":"get_mindsdb_text_to_sql_completion",
143+
"description":tool_description,
144+
"parameters":{
145+
"type":"object",
146+
"properties":{
147+
"model":{
148+
"type":"string",
149+
"description":"llm model name to use for text to sql completion",
150+
},
151+
"data_source":{
152+
"type":"string",
153+
"description":"Data source name",
154+
},
155+
"connection_args":{
156+
"type":"object",
157+
"description":"Connection arguments for the data source",
158+
},
159+
"description":{
160+
"type":"string",
161+
"description":"Description of the data source",
162+
}
163+
},
164+
"required": ['data_source', 'connection_args', 'model', 'description']
165+
}
166+
}
167+
}
168+
169+
112170
def extract_sql_query(result: str) -> str:
113171
"""
114172
Extract the SQL query from an openai result string

0 commit comments

Comments
 (0)