-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmind.py
128 lines (99 loc) · 3.29 KB
/
mind.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import warnings
from pydantic import BaseModel, Field
from typing import List, Optional
from uuid import uuid4
import requests
from logging import getLogger
logger = getLogger(__name__)
# Define the Mind entity
class Mind:
"""
Mind entity
"""
def __init__(self, name):
self.name = name
class DataSourceConfig(BaseModel):
"""
Represents a data source that can be made available to a Mind.
"""
id: str = Field(default_factory=lambda: uuid4().hex)
# Description for underlying agent to know, based on context, whether to access this data source.
description: str
class DatabaseConfig(DataSourceConfig):
"""
Represents a database that can be made available to a Mind.
"""
# Integration name (e.g. postgres)
type: str
# Args for connecting to database.
connection_args: dict
# Tables to make available to the Mind (defaults to ALL).
tables: List[str] = []
class FileConfig(DataSourceConfig):
"""
Represents a collection of files that can be made available to a Mind.
"""
# Local file paths and/or URLs.
paths: List[str]
# TODO: Configure Vector storage. Use defaults for now.
class WebConfig(DataSourceConfig):
"""
Represents a collection of URLs that can be crawled and made available to a Mind.
"""
# Base URLs to crawl from.
urls: List[str]
# Scrapes all URLs found in the starting page (default).
# 0 = scrape provided URLs only
# -1 = no limit (we should set our own sensible limit)
crawl_depth: int = 1
# Include only URLs that match regex patterns.
filters: List[str] = [ ]
# Create mind entity util function
def create_mind(
base_url: str,
api_key: str,
name: str,
data_source_configs: List[DataSourceConfig] = None,
model: Optional[str] = None,
) -> Mind:
"""
Create a mind entity in LiteLLM proxy.
Args:
base_url (str): MindsDB base URL
api_key (str): MindsDB API key
name (str): Mind name
data_source_configs (List[DataSourceConfig]): Data sources to make available to the mind
model: Model orchestrating the AI reasoning loop
Returns:
Mind: Mind entity
"""
warnings.simplefilter('always', DeprecationWarning) # turn off filter
warnings.warn(
'Minds in python SDK are deprecated. Use minds SDK instead (`pip install minds-sdk`)',
category=DeprecationWarning
)
warnings.simplefilter('default', DeprecationWarning)
url = f"{base_url.rstrip('/')}/minds"
headers = {"Authorization": f"Bearer {api_key}"}
if data_source_configs is None:
data_source_configs = []
payload = {
"name": name,
"data_source_configs": [d.model_dump() for d in data_source_configs],
"model": model
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
try:
error_message = e.response.json()
except Exception:
error_message = str(e)
logger.error(f"Failed to create mind: {error_message}")
raise e
except Exception as e:
logger.error(f"Failed to create mind: {e}")
raise e
name = response.json()['name']
return Mind(name=name)