-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathusing_database_mind_text2sql.py
More file actions
53 lines (43 loc) · 1.21 KB
/
using_database_mind_text2sql.py
File metadata and controls
53 lines (43 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from uuid import uuid4
from openai import OpenAI
from mindsdb_sdk.utils.mind import create_mind, DatabaseConfig
import os
# Load MindsDB API key from environment variable. or set it here.
MINDSDB_API_KEY = os.getenv('MINDSDB_API_KEY')
# Set the base URL for the MindsDB LiteLLM proxy.
base_url = 'https://llm.mdb.ai'
# Connect to MindsDB LiteLLM proxy.
client = OpenAI(
api_key=MINDSDB_API_KEY,
base_url=base_url
)
# Create a Database Config.
pg_config = DatabaseConfig(
description='House Sales',
type='postgres',
connection_args={
'user': 'demo_user',
'password': 'demo_password',
'host': 'samples.mindsdb.com',
'port': '5432',
'database': 'demo',
'schema': 'demo_data'
},
tables=['house_sales']
)
# create a database mind
mind = create_mind(
base_url= base_url,
api_key= MINDSDB_API_KEY,
name = f'my_house_data_mind_{uuid4().hex}',
data_source_configs=[pg_config]
)
# Actually pass in our tool to get a SQL completion.
completion = client.chat.completions.create(
model=mind.name,
messages=[
{'role': 'user', 'content': 'How many 2 bedroom houses sold in 2008?'}
],
stream=False
)
print(completion.choices[0].message.content)