asksql 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asksql-2.1.0.dist-info/METADATA +517 -0
- asksql-2.1.0.dist-info/RECORD +291 -0
- asksql-2.1.0.dist-info/WHEEL +4 -0
- asksql-2.1.0.dist-info/entry_points.txt +4 -0
- asksql-2.1.0.dist-info/licenses/LICENSE +21 -0
- vanna/__init__.py +172 -0
- vanna/agents/__init__.py +7 -0
- vanna/capabilities/__init__.py +17 -0
- vanna/capabilities/agent_memory/__init__.py +21 -0
- vanna/capabilities/agent_memory/base.py +103 -0
- vanna/capabilities/agent_memory/models.py +53 -0
- vanna/capabilities/file_system/__init__.py +14 -0
- vanna/capabilities/file_system/base.py +71 -0
- vanna/capabilities/file_system/models.py +25 -0
- vanna/capabilities/sql_runner/__init__.py +13 -0
- vanna/capabilities/sql_runner/base.py +37 -0
- vanna/capabilities/sql_runner/models.py +13 -0
- vanna/components/__init__.py +92 -0
- vanna/components/base.py +11 -0
- vanna/components/rich/__init__.py +83 -0
- vanna/components/rich/containers/__init__.py +7 -0
- vanna/components/rich/containers/card.py +20 -0
- vanna/components/rich/data/__init__.py +9 -0
- vanna/components/rich/data/chart.py +17 -0
- vanna/components/rich/data/dataframe.py +93 -0
- vanna/components/rich/feedback/__init__.py +21 -0
- vanna/components/rich/feedback/badge.py +16 -0
- vanna/components/rich/feedback/icon_text.py +14 -0
- vanna/components/rich/feedback/log_viewer.py +41 -0
- vanna/components/rich/feedback/notification.py +19 -0
- vanna/components/rich/feedback/progress.py +37 -0
- vanna/components/rich/feedback/status_card.py +28 -0
- vanna/components/rich/feedback/status_indicator.py +14 -0
- vanna/components/rich/interactive/__init__.py +21 -0
- vanna/components/rich/interactive/button.py +95 -0
- vanna/components/rich/interactive/task_list.py +58 -0
- vanna/components/rich/interactive/ui_state.py +93 -0
- vanna/components/rich/specialized/__init__.py +7 -0
- vanna/components/rich/specialized/artifact.py +20 -0
- vanna/components/rich/text.py +16 -0
- vanna/components/simple/__init__.py +15 -0
- vanna/components/simple/image.py +15 -0
- vanna/components/simple/link.py +15 -0
- vanna/components/simple/text.py +11 -0
- vanna/core/__init__.py +193 -0
- vanna/core/_compat.py +19 -0
- vanna/core/agent/__init__.py +10 -0
- vanna/core/agent/agent.py +1407 -0
- vanna/core/agent/config.py +123 -0
- vanna/core/audit/__init__.py +28 -0
- vanna/core/audit/base.py +299 -0
- vanna/core/audit/models.py +131 -0
- vanna/core/component_manager.py +329 -0
- vanna/core/components.py +53 -0
- vanna/core/enhancer/__init__.py +11 -0
- vanna/core/enhancer/base.py +94 -0
- vanna/core/enhancer/default.py +118 -0
- vanna/core/enricher/__init__.py +10 -0
- vanna/core/enricher/base.py +59 -0
- vanna/core/errors.py +47 -0
- vanna/core/evaluation/__init__.py +81 -0
- vanna/core/evaluation/base.py +186 -0
- vanna/core/evaluation/dataset.py +254 -0
- vanna/core/evaluation/evaluators.py +376 -0
- vanna/core/evaluation/report.py +289 -0
- vanna/core/evaluation/runner.py +313 -0
- vanna/core/filter/__init__.py +10 -0
- vanna/core/filter/base.py +67 -0
- vanna/core/lifecycle/__init__.py +10 -0
- vanna/core/lifecycle/base.py +83 -0
- vanna/core/llm/__init__.py +16 -0
- vanna/core/llm/base.py +40 -0
- vanna/core/llm/models.py +61 -0
- vanna/core/middleware/__init__.py +10 -0
- vanna/core/middleware/base.py +69 -0
- vanna/core/observability/__init__.py +11 -0
- vanna/core/observability/base.py +88 -0
- vanna/core/observability/models.py +47 -0
- vanna/core/recovery/__init__.py +11 -0
- vanna/core/recovery/base.py +84 -0
- vanna/core/recovery/models.py +32 -0
- vanna/core/registry.py +278 -0
- vanna/core/rich_component.py +156 -0
- vanna/core/simple_component.py +27 -0
- vanna/core/storage/__init__.py +14 -0
- vanna/core/storage/base.py +46 -0
- vanna/core/storage/models.py +46 -0
- vanna/core/system_prompt/__init__.py +13 -0
- vanna/core/system_prompt/base.py +36 -0
- vanna/core/system_prompt/default.py +157 -0
- vanna/core/tool/__init__.py +18 -0
- vanna/core/tool/base.py +70 -0
- vanna/core/tool/models.py +84 -0
- vanna/core/user/__init__.py +17 -0
- vanna/core/user/base.py +29 -0
- vanna/core/user/models.py +25 -0
- vanna/core/user/request_context.py +70 -0
- vanna/core/user/resolver.py +42 -0
- vanna/core/validation.py +164 -0
- vanna/core/workflow/__init__.py +12 -0
- vanna/core/workflow/base.py +254 -0
- vanna/core/workflow/default.py +789 -0
- vanna/examples/__init__.py +1 -0
- vanna/examples/__main__.py +44 -0
- vanna/examples/anthropic_quickstart.py +80 -0
- vanna/examples/artifact_example.py +293 -0
- vanna/examples/claude_sqlite_example.py +236 -0
- vanna/examples/coding_agent_example.py +300 -0
- vanna/examples/custom_system_prompt_example.py +174 -0
- vanna/examples/default_workflow_handler_example.py +208 -0
- vanna/examples/email_auth_example.py +340 -0
- vanna/examples/evaluation_example.py +269 -0
- vanna/examples/extensibility_example.py +262 -0
- vanna/examples/minimal_example.py +67 -0
- vanna/examples/mock_auth_example.py +227 -0
- vanna/examples/mock_custom_tool.py +311 -0
- vanna/examples/mock_quickstart.py +79 -0
- vanna/examples/mock_quota_example.py +145 -0
- vanna/examples/mock_rich_components_demo.py +396 -0
- vanna/examples/mock_sqlite_example.py +223 -0
- vanna/examples/openai_quickstart.py +83 -0
- vanna/examples/primitive_components_demo.py +305 -0
- vanna/examples/quota_lifecycle_example.py +139 -0
- vanna/examples/visualization_example.py +251 -0
- vanna/integrations/__init__.py +17 -0
- vanna/integrations/anthropic/__init__.py +9 -0
- vanna/integrations/anthropic/llm.py +270 -0
- vanna/integrations/azureopenai/__init__.py +9 -0
- vanna/integrations/azureopenai/llm.py +329 -0
- vanna/integrations/azuresearch/__init__.py +7 -0
- vanna/integrations/azuresearch/agent_memory.py +413 -0
- vanna/integrations/bigquery/__init__.py +5 -0
- vanna/integrations/bigquery/sql_runner.py +81 -0
- vanna/integrations/chromadb/__init__.py +104 -0
- vanna/integrations/chromadb/agent_memory.py +488 -0
- vanna/integrations/clickhouse/__init__.py +5 -0
- vanna/integrations/clickhouse/sql_runner.py +82 -0
- vanna/integrations/duckdb/__init__.py +5 -0
- vanna/integrations/duckdb/sql_runner.py +65 -0
- vanna/integrations/faiss/__init__.py +7 -0
- vanna/integrations/faiss/agent_memory.py +435 -0
- vanna/integrations/google/__init__.py +9 -0
- vanna/integrations/google/gemini.py +370 -0
- vanna/integrations/hive/__init__.py +5 -0
- vanna/integrations/hive/sql_runner.py +87 -0
- vanna/integrations/local/__init__.py +17 -0
- vanna/integrations/local/agent_memory/__init__.py +7 -0
- vanna/integrations/local/agent_memory/in_memory.py +285 -0
- vanna/integrations/local/audit.py +59 -0
- vanna/integrations/local/file_system.py +242 -0
- vanna/integrations/local/file_system_conversation_store.py +255 -0
- vanna/integrations/local/storage.py +62 -0
- vanna/integrations/marqo/__init__.py +7 -0
- vanna/integrations/marqo/agent_memory.py +354 -0
- vanna/integrations/milvus/__init__.py +7 -0
- vanna/integrations/milvus/agent_memory.py +458 -0
- vanna/integrations/mock/__init__.py +9 -0
- vanna/integrations/mock/llm.py +65 -0
- vanna/integrations/mssql/__init__.py +5 -0
- vanna/integrations/mssql/sql_runner.py +66 -0
- vanna/integrations/mysql/__init__.py +5 -0
- vanna/integrations/mysql/sql_runner.py +92 -0
- vanna/integrations/ollama/__init__.py +7 -0
- vanna/integrations/ollama/llm.py +252 -0
- vanna/integrations/openai/__init__.py +10 -0
- vanna/integrations/openai/llm.py +267 -0
- vanna/integrations/openai/responses.py +163 -0
- vanna/integrations/opensearch/__init__.py +7 -0
- vanna/integrations/opensearch/agent_memory.py +411 -0
- vanna/integrations/oracle/__init__.py +5 -0
- vanna/integrations/oracle/sql_runner.py +75 -0
- vanna/integrations/pinecone/__init__.py +7 -0
- vanna/integrations/pinecone/agent_memory.py +329 -0
- vanna/integrations/plotly/__init__.py +5 -0
- vanna/integrations/plotly/chart_generator.py +313 -0
- vanna/integrations/postgres/__init__.py +9 -0
- vanna/integrations/postgres/sql_runner.py +112 -0
- vanna/integrations/premium/agent_memory/__init__.py +7 -0
- vanna/integrations/premium/agent_memory/premium.py +186 -0
- vanna/integrations/presto/__init__.py +5 -0
- vanna/integrations/presto/sql_runner.py +107 -0
- vanna/integrations/qdrant/__init__.py +7 -0
- vanna/integrations/qdrant/agent_memory.py +466 -0
- vanna/integrations/snowflake/__init__.py +5 -0
- vanna/integrations/snowflake/sql_runner.py +147 -0
- vanna/integrations/sqlite/__init__.py +9 -0
- vanna/integrations/sqlite/sql_runner.py +65 -0
- vanna/integrations/weaviate/__init__.py +7 -0
- vanna/integrations/weaviate/agent_memory.py +428 -0
- vanna/legacy/ZhipuAI/ZhipuAI_Chat.py +233 -0
- vanna/legacy/ZhipuAI/ZhipuAI_embeddings.py +79 -0
- vanna/legacy/ZhipuAI/__init__.py +2 -0
- vanna/legacy/__init__.py +403 -0
- vanna/legacy/adapter.py +468 -0
- vanna/legacy/advanced/__init__.py +28 -0
- vanna/legacy/anthropic/__init__.py +1 -0
- vanna/legacy/anthropic/anthropic_chat.py +80 -0
- vanna/legacy/azuresearch/__init__.py +1 -0
- vanna/legacy/azuresearch/azuresearch_vector.py +274 -0
- vanna/legacy/base/__init__.py +1 -0
- vanna/legacy/base/base.py +2133 -0
- vanna/legacy/base/safe_exec.py +190 -0
- vanna/legacy/bedrock/__init__.py +1 -0
- vanna/legacy/bedrock/bedrock_converse.py +86 -0
- vanna/legacy/chromadb/__init__.py +1 -0
- vanna/legacy/chromadb/chromadb_vector.py +259 -0
- vanna/legacy/cohere/__init__.py +2 -0
- vanna/legacy/cohere/cohere_chat.py +99 -0
- vanna/legacy/cohere/cohere_embeddings.py +77 -0
- vanna/legacy/deepseek/__init__.py +1 -0
- vanna/legacy/deepseek/deepseek_chat.py +59 -0
- vanna/legacy/exceptions/__init__.py +46 -0
- vanna/legacy/faiss/__init__.py +1 -0
- vanna/legacy/faiss/faiss.py +230 -0
- vanna/legacy/flask/__init__.py +1355 -0
- vanna/legacy/flask/assets.py +58 -0
- vanna/legacy/flask/auth.py +56 -0
- vanna/legacy/google/__init__.py +2 -0
- vanna/legacy/google/bigquery_vector.py +293 -0
- vanna/legacy/google/gemini_chat.py +78 -0
- vanna/legacy/hf/__init__.py +1 -0
- vanna/legacy/hf/hf.py +80 -0
- vanna/legacy/local.py +8 -0
- vanna/legacy/marqo/__init__.py +1 -0
- vanna/legacy/marqo/marqo.py +168 -0
- vanna/legacy/milvus/__init__.py +1 -0
- vanna/legacy/milvus/milvus_vector.py +328 -0
- vanna/legacy/mistral/__init__.py +1 -0
- vanna/legacy/mistral/mistral.py +51 -0
- vanna/legacy/mock/__init__.py +3 -0
- vanna/legacy/mock/embedding.py +11 -0
- vanna/legacy/mock/llm.py +18 -0
- vanna/legacy/mock/vectordb.py +67 -0
- vanna/legacy/ollama/__init__.py +1 -0
- vanna/legacy/ollama/ollama.py +110 -0
- vanna/legacy/openai/__init__.py +2 -0
- vanna/legacy/openai/openai_chat.py +124 -0
- vanna/legacy/openai/openai_embeddings.py +46 -0
- vanna/legacy/opensearch/__init__.py +2 -0
- vanna/legacy/opensearch/opensearch_vector.py +369 -0
- vanna/legacy/opensearch/opensearch_vector_semantic.py +200 -0
- vanna/legacy/oracle/__init__.py +1 -0
- vanna/legacy/oracle/oracle_vector.py +584 -0
- vanna/legacy/pgvector/__init__.py +1 -0
- vanna/legacy/pgvector/pgvector.py +282 -0
- vanna/legacy/pinecone/__init__.py +3 -0
- vanna/legacy/pinecone/pinecone_vector.py +275 -0
- vanna/legacy/qdrant/__init__.py +3 -0
- vanna/legacy/qdrant/qdrant.py +329 -0
- vanna/legacy/qianfan/Qianfan_Chat.py +170 -0
- vanna/legacy/qianfan/Qianfan_embeddings.py +36 -0
- vanna/legacy/qianfan/__init__.py +2 -0
- vanna/legacy/qianwen/QianwenAI_chat.py +132 -0
- vanna/legacy/qianwen/QianwenAI_embeddings.py +46 -0
- vanna/legacy/qianwen/__init__.py +2 -0
- vanna/legacy/remote.py +79 -0
- vanna/legacy/types/__init__.py +292 -0
- vanna/legacy/utils.py +72 -0
- vanna/legacy/vannadb/__init__.py +1 -0
- vanna/legacy/vannadb/vannadb_vector.py +468 -0
- vanna/legacy/vllm/__init__.py +1 -0
- vanna/legacy/vllm/vllm.py +97 -0
- vanna/legacy/weaviate/__init__.py +1 -0
- vanna/legacy/weaviate/weaviate_vector.py +194 -0
- vanna/legacy/xinference/__init__.py +1 -0
- vanna/legacy/xinference/xinference.py +53 -0
- vanna/py.typed +0 -0
- vanna/servers/__init__.py +16 -0
- vanna/servers/__main__.py +8 -0
- vanna/servers/base/__init__.py +18 -0
- vanna/servers/base/chat_handler.py +65 -0
- vanna/servers/base/models.py +111 -0
- vanna/servers/base/rich_chat_handler.py +141 -0
- vanna/servers/base/templates.py +331 -0
- vanna/servers/cli/__init__.py +7 -0
- vanna/servers/cli/server_runner.py +204 -0
- vanna/servers/fastapi/__init__.py +7 -0
- vanna/servers/fastapi/app.py +163 -0
- vanna/servers/fastapi/routes.py +183 -0
- vanna/servers/flask/__init__.py +7 -0
- vanna/servers/flask/app.py +132 -0
- vanna/servers/flask/routes.py +137 -0
- vanna/tools/__init__.py +41 -0
- vanna/tools/agent_memory.py +322 -0
- vanna/tools/file_system.py +879 -0
- vanna/tools/python.py +222 -0
- vanna/tools/run_sql.py +173 -0
- vanna/tools/visualize_data.py +195 -0
- vanna/utils/__init__.py +0 -0
- vanna/utils/sql_safety.py +84 -0
- vanna/web_components/__init__.py +44 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .milvus_vector import Milvus_VectorStore
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
import pandas as pd
|
|
5
|
+
from pymilvus import DataType, MilvusClient, model
|
|
6
|
+
|
|
7
|
+
from ..base import VannaBase
|
|
8
|
+
|
|
9
|
+
# Setting the URI as a local file, e.g.`./milvus.db`,
|
|
10
|
+
# is the most convenient method, as it automatically utilizes Milvus Lite
|
|
11
|
+
# to store all data in this file.
|
|
12
|
+
#
|
|
13
|
+
# If you have large scale of data such as more than a million docs, we
|
|
14
|
+
# recommend setting up a more performant Milvus server on docker or kubernetes.
|
|
15
|
+
# When using this setup, please use the server URI,
|
|
16
|
+
# e.g.`http://localhost:19530`, as your URI.
|
|
17
|
+
|
|
18
|
+
DEFAULT_MILVUS_URI = "./milvus.db"
|
|
19
|
+
# DEFAULT_MILVUS_URI = "http://localhost:19530"
|
|
20
|
+
|
|
21
|
+
MAX_LIMIT_SIZE = 10_000
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Milvus_VectorStore(VannaBase):
|
|
25
|
+
"""
|
|
26
|
+
Vectorstore implementation using Milvus - https://milvus.io/docs/quickstart.md
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
- config (dict, optional): Dictionary of `Milvus_VectorStore config` options. Defaults to `None`.
|
|
30
|
+
- milvus_client: A `pymilvus.MilvusClient` instance.
|
|
31
|
+
- embedding_function:
|
|
32
|
+
A `milvus_model.base.BaseEmbeddingFunction` instance. Defaults to `DefaultEmbeddingFunction()`.
|
|
33
|
+
For more models, please refer to:
|
|
34
|
+
https://milvus.io/docs/embeddings.md
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, config=None):
|
|
38
|
+
VannaBase.__init__(self, config=config)
|
|
39
|
+
|
|
40
|
+
if "milvus_client" in config:
|
|
41
|
+
self.milvus_client = config["milvus_client"]
|
|
42
|
+
else:
|
|
43
|
+
self.milvus_client = MilvusClient(uri=DEFAULT_MILVUS_URI)
|
|
44
|
+
|
|
45
|
+
if "embedding_function" in config:
|
|
46
|
+
self.embedding_function = config.get("embedding_function")
|
|
47
|
+
else:
|
|
48
|
+
self.embedding_function = model.DefaultEmbeddingFunction()
|
|
49
|
+
self._embedding_dim = self.embedding_function.encode_documents(["foo"])[
|
|
50
|
+
0
|
|
51
|
+
].shape[0]
|
|
52
|
+
self._create_collections()
|
|
53
|
+
self.n_results = config.get("n_results", 10)
|
|
54
|
+
|
|
55
|
+
def _create_collections(self):
|
|
56
|
+
self._create_sql_collection("vannasql")
|
|
57
|
+
self._create_ddl_collection("vannaddl")
|
|
58
|
+
self._create_doc_collection("vannadoc")
|
|
59
|
+
|
|
60
|
+
def generate_embedding(self, data: str, **kwargs) -> List[float]:
|
|
61
|
+
return self.embedding_function.encode_documents(data).tolist()
|
|
62
|
+
|
|
63
|
+
def _create_sql_collection(self, name: str):
|
|
64
|
+
if not self.milvus_client.has_collection(collection_name=name):
|
|
65
|
+
vannasql_schema = MilvusClient.create_schema(
|
|
66
|
+
auto_id=False,
|
|
67
|
+
enable_dynamic_field=False,
|
|
68
|
+
)
|
|
69
|
+
vannasql_schema.add_field(
|
|
70
|
+
field_name="id",
|
|
71
|
+
datatype=DataType.VARCHAR,
|
|
72
|
+
max_length=65535,
|
|
73
|
+
is_primary=True,
|
|
74
|
+
)
|
|
75
|
+
vannasql_schema.add_field(
|
|
76
|
+
field_name="text", datatype=DataType.VARCHAR, max_length=65535
|
|
77
|
+
)
|
|
78
|
+
vannasql_schema.add_field(
|
|
79
|
+
field_name="sql", datatype=DataType.VARCHAR, max_length=65535
|
|
80
|
+
)
|
|
81
|
+
vannasql_schema.add_field(
|
|
82
|
+
field_name="vector",
|
|
83
|
+
datatype=DataType.FLOAT_VECTOR,
|
|
84
|
+
dim=self._embedding_dim,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
vannasql_index_params = self.milvus_client.prepare_index_params()
|
|
88
|
+
vannasql_index_params.add_index(
|
|
89
|
+
field_name="vector",
|
|
90
|
+
index_name="vector",
|
|
91
|
+
index_type="AUTOINDEX",
|
|
92
|
+
metric_type="L2",
|
|
93
|
+
)
|
|
94
|
+
self.milvus_client.create_collection(
|
|
95
|
+
collection_name=name,
|
|
96
|
+
schema=vannasql_schema,
|
|
97
|
+
index_params=vannasql_index_params,
|
|
98
|
+
consistency_level="Strong",
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
def _create_ddl_collection(self, name: str):
|
|
102
|
+
if not self.milvus_client.has_collection(collection_name=name):
|
|
103
|
+
vannaddl_schema = MilvusClient.create_schema(
|
|
104
|
+
auto_id=False,
|
|
105
|
+
enable_dynamic_field=False,
|
|
106
|
+
)
|
|
107
|
+
vannaddl_schema.add_field(
|
|
108
|
+
field_name="id",
|
|
109
|
+
datatype=DataType.VARCHAR,
|
|
110
|
+
max_length=65535,
|
|
111
|
+
is_primary=True,
|
|
112
|
+
)
|
|
113
|
+
vannaddl_schema.add_field(
|
|
114
|
+
field_name="ddl", datatype=DataType.VARCHAR, max_length=65535
|
|
115
|
+
)
|
|
116
|
+
vannaddl_schema.add_field(
|
|
117
|
+
field_name="vector",
|
|
118
|
+
datatype=DataType.FLOAT_VECTOR,
|
|
119
|
+
dim=self._embedding_dim,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
vannaddl_index_params = self.milvus_client.prepare_index_params()
|
|
123
|
+
vannaddl_index_params.add_index(
|
|
124
|
+
field_name="vector",
|
|
125
|
+
index_name="vector",
|
|
126
|
+
index_type="AUTOINDEX",
|
|
127
|
+
metric_type="L2",
|
|
128
|
+
)
|
|
129
|
+
self.milvus_client.create_collection(
|
|
130
|
+
collection_name=name,
|
|
131
|
+
schema=vannaddl_schema,
|
|
132
|
+
index_params=vannaddl_index_params,
|
|
133
|
+
consistency_level="Strong",
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
def _create_doc_collection(self, name: str):
|
|
137
|
+
if not self.milvus_client.has_collection(collection_name=name):
|
|
138
|
+
vannadoc_schema = MilvusClient.create_schema(
|
|
139
|
+
auto_id=False,
|
|
140
|
+
enable_dynamic_field=False,
|
|
141
|
+
)
|
|
142
|
+
vannadoc_schema.add_field(
|
|
143
|
+
field_name="id",
|
|
144
|
+
datatype=DataType.VARCHAR,
|
|
145
|
+
max_length=65535,
|
|
146
|
+
is_primary=True,
|
|
147
|
+
)
|
|
148
|
+
vannadoc_schema.add_field(
|
|
149
|
+
field_name="doc", datatype=DataType.VARCHAR, max_length=65535
|
|
150
|
+
)
|
|
151
|
+
vannadoc_schema.add_field(
|
|
152
|
+
field_name="vector",
|
|
153
|
+
datatype=DataType.FLOAT_VECTOR,
|
|
154
|
+
dim=self._embedding_dim,
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
vannadoc_index_params = self.milvus_client.prepare_index_params()
|
|
158
|
+
vannadoc_index_params.add_index(
|
|
159
|
+
field_name="vector",
|
|
160
|
+
index_name="vector",
|
|
161
|
+
index_type="AUTOINDEX",
|
|
162
|
+
metric_type="L2",
|
|
163
|
+
)
|
|
164
|
+
self.milvus_client.create_collection(
|
|
165
|
+
collection_name=name,
|
|
166
|
+
schema=vannadoc_schema,
|
|
167
|
+
index_params=vannadoc_index_params,
|
|
168
|
+
consistency_level="Strong",
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
def add_question_sql(self, question: str, sql: str, **kwargs) -> str:
|
|
172
|
+
if len(question) == 0 or len(sql) == 0:
|
|
173
|
+
raise Exception("pair of question and sql can not be null")
|
|
174
|
+
_id = str(uuid.uuid4()) + "-sql"
|
|
175
|
+
embedding = self.embedding_function.encode_documents([question])[0]
|
|
176
|
+
self.milvus_client.insert(
|
|
177
|
+
collection_name="vannasql",
|
|
178
|
+
data={"id": _id, "text": question, "sql": sql, "vector": embedding},
|
|
179
|
+
)
|
|
180
|
+
return _id
|
|
181
|
+
|
|
182
|
+
def add_ddl(self, ddl: str, **kwargs) -> str:
|
|
183
|
+
if len(ddl) == 0:
|
|
184
|
+
raise Exception("ddl can not be null")
|
|
185
|
+
_id = str(uuid.uuid4()) + "-ddl"
|
|
186
|
+
embedding = self.embedding_function.encode_documents([ddl])[0]
|
|
187
|
+
self.milvus_client.insert(
|
|
188
|
+
collection_name="vannaddl",
|
|
189
|
+
data={"id": _id, "ddl": ddl, "vector": embedding},
|
|
190
|
+
)
|
|
191
|
+
return _id
|
|
192
|
+
|
|
193
|
+
def add_documentation(self, documentation: str, **kwargs) -> str:
|
|
194
|
+
if len(documentation) == 0:
|
|
195
|
+
raise Exception("documentation can not be null")
|
|
196
|
+
_id = str(uuid.uuid4()) + "-doc"
|
|
197
|
+
embedding = self.embedding_function.encode_documents([documentation])[0]
|
|
198
|
+
self.milvus_client.insert(
|
|
199
|
+
collection_name="vannadoc",
|
|
200
|
+
data={"id": _id, "doc": documentation, "vector": embedding},
|
|
201
|
+
)
|
|
202
|
+
return _id
|
|
203
|
+
|
|
204
|
+
def get_training_data(self, **kwargs) -> pd.DataFrame:
|
|
205
|
+
sql_data = self.milvus_client.query(
|
|
206
|
+
collection_name="vannasql",
|
|
207
|
+
output_fields=["*"],
|
|
208
|
+
limit=MAX_LIMIT_SIZE,
|
|
209
|
+
)
|
|
210
|
+
df = pd.DataFrame()
|
|
211
|
+
df_sql = pd.DataFrame(
|
|
212
|
+
{
|
|
213
|
+
"id": [doc["id"] for doc in sql_data],
|
|
214
|
+
"question": [doc["text"] for doc in sql_data],
|
|
215
|
+
"content": [doc["sql"] for doc in sql_data],
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
df = pd.concat([df, df_sql])
|
|
219
|
+
|
|
220
|
+
ddl_data = self.milvus_client.query(
|
|
221
|
+
collection_name="vannaddl",
|
|
222
|
+
output_fields=["*"],
|
|
223
|
+
limit=MAX_LIMIT_SIZE,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
df_ddl = pd.DataFrame(
|
|
227
|
+
{
|
|
228
|
+
"id": [doc["id"] for doc in ddl_data],
|
|
229
|
+
"question": [None for doc in ddl_data],
|
|
230
|
+
"content": [doc["ddl"] for doc in ddl_data],
|
|
231
|
+
}
|
|
232
|
+
)
|
|
233
|
+
df = pd.concat([df, df_ddl])
|
|
234
|
+
|
|
235
|
+
doc_data = self.milvus_client.query(
|
|
236
|
+
collection_name="vannadoc",
|
|
237
|
+
output_fields=["*"],
|
|
238
|
+
limit=MAX_LIMIT_SIZE,
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
df_doc = pd.DataFrame(
|
|
242
|
+
{
|
|
243
|
+
"id": [doc["id"] for doc in doc_data],
|
|
244
|
+
"question": [None for doc in doc_data],
|
|
245
|
+
"content": [doc["doc"] for doc in doc_data],
|
|
246
|
+
}
|
|
247
|
+
)
|
|
248
|
+
df = pd.concat([df, df_doc])
|
|
249
|
+
return df
|
|
250
|
+
|
|
251
|
+
def get_similar_question_sql(self, question: str, **kwargs) -> list:
|
|
252
|
+
search_params = {
|
|
253
|
+
"metric_type": "L2",
|
|
254
|
+
"params": {"nprobe": 128},
|
|
255
|
+
}
|
|
256
|
+
embeddings = self.embedding_function.encode_queries([question])
|
|
257
|
+
res = self.milvus_client.search(
|
|
258
|
+
collection_name="vannasql",
|
|
259
|
+
anns_field="vector",
|
|
260
|
+
data=embeddings,
|
|
261
|
+
limit=self.n_results,
|
|
262
|
+
output_fields=["text", "sql"],
|
|
263
|
+
search_params=search_params,
|
|
264
|
+
)
|
|
265
|
+
res = res[0]
|
|
266
|
+
|
|
267
|
+
list_sql = []
|
|
268
|
+
for doc in res:
|
|
269
|
+
dict = {}
|
|
270
|
+
dict["question"] = doc["entity"]["text"]
|
|
271
|
+
dict["sql"] = doc["entity"]["sql"]
|
|
272
|
+
list_sql.append(dict)
|
|
273
|
+
return list_sql
|
|
274
|
+
|
|
275
|
+
def get_related_ddl(self, question: str, **kwargs) -> list:
|
|
276
|
+
search_params = {
|
|
277
|
+
"metric_type": "L2",
|
|
278
|
+
"params": {"nprobe": 128},
|
|
279
|
+
}
|
|
280
|
+
embeddings = self.embedding_function.encode_queries([question])
|
|
281
|
+
res = self.milvus_client.search(
|
|
282
|
+
collection_name="vannaddl",
|
|
283
|
+
anns_field="vector",
|
|
284
|
+
data=embeddings,
|
|
285
|
+
limit=self.n_results,
|
|
286
|
+
output_fields=["ddl"],
|
|
287
|
+
search_params=search_params,
|
|
288
|
+
)
|
|
289
|
+
res = res[0]
|
|
290
|
+
|
|
291
|
+
list_ddl = []
|
|
292
|
+
for doc in res:
|
|
293
|
+
list_ddl.append(doc["entity"]["ddl"])
|
|
294
|
+
return list_ddl
|
|
295
|
+
|
|
296
|
+
def get_related_documentation(self, question: str, **kwargs) -> list:
|
|
297
|
+
search_params = {
|
|
298
|
+
"metric_type": "L2",
|
|
299
|
+
"params": {"nprobe": 128},
|
|
300
|
+
}
|
|
301
|
+
embeddings = self.embedding_function.encode_queries([question])
|
|
302
|
+
res = self.milvus_client.search(
|
|
303
|
+
collection_name="vannadoc",
|
|
304
|
+
anns_field="vector",
|
|
305
|
+
data=embeddings,
|
|
306
|
+
limit=self.n_results,
|
|
307
|
+
output_fields=["doc"],
|
|
308
|
+
search_params=search_params,
|
|
309
|
+
)
|
|
310
|
+
res = res[0]
|
|
311
|
+
|
|
312
|
+
list_doc = []
|
|
313
|
+
for doc in res:
|
|
314
|
+
list_doc.append(doc["entity"]["doc"])
|
|
315
|
+
return list_doc
|
|
316
|
+
|
|
317
|
+
def remove_training_data(self, id: str, **kwargs) -> bool:
|
|
318
|
+
if id.endswith("-sql"):
|
|
319
|
+
self.milvus_client.delete(collection_name="vannasql", ids=[id])
|
|
320
|
+
return True
|
|
321
|
+
elif id.endswith("-ddl"):
|
|
322
|
+
self.milvus_client.delete(collection_name="vannaddl", ids=[id])
|
|
323
|
+
return True
|
|
324
|
+
elif id.endswith("-doc"):
|
|
325
|
+
self.milvus_client.delete(collection_name="vannadoc", ids=[id])
|
|
326
|
+
return True
|
|
327
|
+
else:
|
|
328
|
+
return False
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .mistral import Mistral
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from mistralai import Mistral as MistralClient
|
|
4
|
+
from mistralai import UserMessage
|
|
5
|
+
|
|
6
|
+
from ..base import VannaBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Mistral(VannaBase):
|
|
10
|
+
def __init__(self, config=None):
|
|
11
|
+
if config is None:
|
|
12
|
+
raise ValueError(
|
|
13
|
+
"For Mistral, config must be provided with an api_key and model"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
if "api_key" not in config:
|
|
17
|
+
raise ValueError("config must contain a Mistral api_key")
|
|
18
|
+
|
|
19
|
+
if "model" not in config:
|
|
20
|
+
raise ValueError("config must contain a Mistral model")
|
|
21
|
+
|
|
22
|
+
api_key = config["api_key"]
|
|
23
|
+
model = config["model"]
|
|
24
|
+
self.client = MistralClient(api_key=api_key)
|
|
25
|
+
self.model = model
|
|
26
|
+
|
|
27
|
+
def system_message(self, message: str) -> any:
|
|
28
|
+
return {"role": "system", "content": message}
|
|
29
|
+
|
|
30
|
+
def user_message(self, message: str) -> any:
|
|
31
|
+
return {"role": "user", "content": message}
|
|
32
|
+
|
|
33
|
+
def assistant_message(self, message: str) -> any:
|
|
34
|
+
return {"role": "assistant", "content": message}
|
|
35
|
+
|
|
36
|
+
def generate_sql(self, question: str, **kwargs) -> str:
|
|
37
|
+
# Use the super generate_sql
|
|
38
|
+
sql = super().generate_sql(question, **kwargs)
|
|
39
|
+
|
|
40
|
+
# Replace "\_" with "_"
|
|
41
|
+
sql = sql.replace("\\_", "_")
|
|
42
|
+
|
|
43
|
+
return sql
|
|
44
|
+
|
|
45
|
+
def submit_prompt(self, prompt, **kwargs) -> str:
|
|
46
|
+
chat_response = self.client.chat.complete(
|
|
47
|
+
model=self.model,
|
|
48
|
+
messages=prompt,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
return chat_response.choices[0].message.content
|
vanna/legacy/mock/llm.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from ..base import VannaBase
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MockLLM(VannaBase):
|
|
5
|
+
def __init__(self, config=None):
|
|
6
|
+
pass
|
|
7
|
+
|
|
8
|
+
def system_message(self, message: str) -> any:
|
|
9
|
+
return {"role": "system", "content": message}
|
|
10
|
+
|
|
11
|
+
def user_message(self, message: str) -> any:
|
|
12
|
+
return {"role": "user", "content": message}
|
|
13
|
+
|
|
14
|
+
def assistant_message(self, message: str) -> any:
|
|
15
|
+
return {"role": "assistant", "content": message}
|
|
16
|
+
|
|
17
|
+
def submit_prompt(self, prompt, **kwargs) -> str:
|
|
18
|
+
return "Mock LLM response"
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
from ..base import VannaBase
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MockVectorDB(VannaBase):
|
|
7
|
+
def __init__(self, config=None):
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
def _get_id(self, value: str, **kwargs) -> str:
|
|
11
|
+
# Hash the value and return the ID
|
|
12
|
+
return str(hash(value))
|
|
13
|
+
|
|
14
|
+
def add_ddl(self, ddl: str, **kwargs) -> str:
|
|
15
|
+
return self._get_id(ddl)
|
|
16
|
+
|
|
17
|
+
def add_documentation(self, doc: str, **kwargs) -> str:
|
|
18
|
+
return self._get_id(doc)
|
|
19
|
+
|
|
20
|
+
def add_question_sql(self, question: str, sql: str, **kwargs) -> str:
|
|
21
|
+
return self._get_id(question)
|
|
22
|
+
|
|
23
|
+
def get_related_ddl(self, question: str, **kwargs) -> list:
|
|
24
|
+
return []
|
|
25
|
+
|
|
26
|
+
def get_related_documentation(self, question: str, **kwargs) -> list:
|
|
27
|
+
return []
|
|
28
|
+
|
|
29
|
+
def get_similar_question_sql(self, question: str, **kwargs) -> list:
|
|
30
|
+
return []
|
|
31
|
+
|
|
32
|
+
def get_training_data(self, **kwargs) -> pd.DataFrame:
|
|
33
|
+
return pd.DataFrame(
|
|
34
|
+
{
|
|
35
|
+
"id": {
|
|
36
|
+
0: "19546-ddl",
|
|
37
|
+
1: "91597-sql",
|
|
38
|
+
2: "133976-sql",
|
|
39
|
+
3: "59851-doc",
|
|
40
|
+
4: "73046-sql",
|
|
41
|
+
},
|
|
42
|
+
"training_data_type": {
|
|
43
|
+
0: "ddl",
|
|
44
|
+
1: "sql",
|
|
45
|
+
2: "sql",
|
|
46
|
+
3: "documentation",
|
|
47
|
+
4: "sql",
|
|
48
|
+
},
|
|
49
|
+
"question": {
|
|
50
|
+
0: None,
|
|
51
|
+
1: "What are the top selling genres?",
|
|
52
|
+
2: "What are the low 7 artists by sales?",
|
|
53
|
+
3: None,
|
|
54
|
+
4: "What is the total sales for each customer?",
|
|
55
|
+
},
|
|
56
|
+
"content": {
|
|
57
|
+
0: "CREATE TABLE [Invoice]\n(\n [InvoiceId] INTEGER NOT NULL,\n [CustomerId] INTEGER NOT NULL,\n [InvoiceDate] DATETIME NOT NULL,\n [BillingAddress] NVARCHAR(70),\n [BillingCity] NVARCHAR(40),\n [BillingState] NVARCHAR(40),\n [BillingCountry] NVARCHAR(40),\n [BillingPostalCode] NVARCHAR(10),\n [Total] NUMERIC(10,2) NOT NULL,\n CONSTRAINT [PK_Invoice] PRIMARY KEY ([InvoiceId]),\n FOREIGN KEY ([CustomerId]) REFERENCES [Customer] ([CustomerId]) \n\t\tON DELETE NO ACTION ON UPDATE NO ACTION\n)",
|
|
58
|
+
1: "SELECT g.Name AS Genre, SUM(il.Quantity) AS TotalSales\nFROM Genre g\nJOIN Track t ON g.GenreId = t.GenreId\nJOIN InvoiceLine il ON t.TrackId = il.TrackId\nGROUP BY g.GenreId, g.Name\nORDER BY TotalSales DESC;",
|
|
59
|
+
2: "SELECT a.ArtistId, a.Name, SUM(il.Quantity) AS TotalSales\nFROM Artist a\nINNER JOIN Album al ON a.ArtistId = al.ArtistId\nINNER JOIN Track t ON al.AlbumId = t.AlbumId\nINNER JOIN InvoiceLine il ON t.TrackId = il.TrackId\nGROUP BY a.ArtistId, a.Name\nORDER BY TotalSales ASC\nLIMIT 7;",
|
|
60
|
+
3: "This is a SQLite database. For dates rememeber to use SQLite syntax.",
|
|
61
|
+
4: "SELECT c.CustomerId, c.FirstName, c.LastName, SUM(i.Total) AS TotalSales\nFROM Customer c\nJOIN Invoice i ON c.CustomerId = i.CustomerId\nGROUP BY c.CustomerId, c.FirstName, c.LastName;",
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def remove_training_data(id: str, **kwargs) -> bool:
|
|
67
|
+
return True
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .ollama import Ollama
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
from httpx import Timeout
|
|
5
|
+
|
|
6
|
+
from ..base import VannaBase
|
|
7
|
+
from ..exceptions import DependencyError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Ollama(VannaBase):
|
|
11
|
+
def __init__(self, config=None):
|
|
12
|
+
try:
|
|
13
|
+
ollama = __import__("ollama")
|
|
14
|
+
except ImportError:
|
|
15
|
+
raise DependencyError(
|
|
16
|
+
"You need to install required dependencies to execute this method, run command:"
|
|
17
|
+
" \npip install ollama"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
if not config:
|
|
21
|
+
raise ValueError("config must contain at least Ollama model")
|
|
22
|
+
if "model" not in config.keys():
|
|
23
|
+
raise ValueError("config must contain at least Ollama model")
|
|
24
|
+
self.host = config.get("ollama_host", "http://localhost:11434")
|
|
25
|
+
self.model = config["model"]
|
|
26
|
+
if ":" not in self.model:
|
|
27
|
+
self.model += ":latest"
|
|
28
|
+
|
|
29
|
+
self.ollama_timeout = config.get("ollama_timeout", 240.0)
|
|
30
|
+
|
|
31
|
+
self.ollama_client = ollama.Client(
|
|
32
|
+
self.host, timeout=Timeout(self.ollama_timeout)
|
|
33
|
+
)
|
|
34
|
+
self.keep_alive = config.get("keep_alive", None)
|
|
35
|
+
self.ollama_options = config.get("options", {})
|
|
36
|
+
self.num_ctx = self.ollama_options.get("num_ctx", 2048)
|
|
37
|
+
self.__pull_model_if_ne(self.ollama_client, self.model)
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def __pull_model_if_ne(ollama_client, model):
|
|
41
|
+
model_response = ollama_client.list()
|
|
42
|
+
model_lists = [
|
|
43
|
+
model_element["model"] for model_element in model_response.get("models", [])
|
|
44
|
+
]
|
|
45
|
+
if model not in model_lists:
|
|
46
|
+
ollama_client.pull(model)
|
|
47
|
+
|
|
48
|
+
def system_message(self, message: str) -> any:
|
|
49
|
+
return {"role": "system", "content": message}
|
|
50
|
+
|
|
51
|
+
def user_message(self, message: str) -> any:
|
|
52
|
+
return {"role": "user", "content": message}
|
|
53
|
+
|
|
54
|
+
def assistant_message(self, message: str) -> any:
|
|
55
|
+
return {"role": "assistant", "content": message}
|
|
56
|
+
|
|
57
|
+
def extract_sql(self, llm_response):
|
|
58
|
+
"""
|
|
59
|
+
Extracts the first SQL statement after the word 'select', ignoring case,
|
|
60
|
+
matches until the first semicolon, three backticks, or the end of the string,
|
|
61
|
+
and removes three backticks if they exist in the extracted string.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
- llm_response (str): The string to search within for an SQL statement.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
- str: The first SQL statement found, with three backticks removed, or an empty string if no match is found.
|
|
68
|
+
"""
|
|
69
|
+
# Remove ollama-generated extra characters
|
|
70
|
+
llm_response = llm_response.replace("\\_", "_")
|
|
71
|
+
llm_response = llm_response.replace("\\", "")
|
|
72
|
+
|
|
73
|
+
# Regular expression to find ```sql' and capture until '```'
|
|
74
|
+
sql = re.search(r"```sql\n((.|\n)*?)(?=;|\[|```)", llm_response, re.DOTALL)
|
|
75
|
+
# Regular expression to find 'select, with (ignoring case) and capture until ';', [ (this happens in case of mistral) or end of string
|
|
76
|
+
select_with = re.search(
|
|
77
|
+
r"(select|(with.*?as \())(.*?)(?=;|\[|```)",
|
|
78
|
+
llm_response,
|
|
79
|
+
re.IGNORECASE | re.DOTALL,
|
|
80
|
+
)
|
|
81
|
+
if sql:
|
|
82
|
+
self.log(f"Output from LLM: {llm_response} \nExtracted SQL: {sql.group(1)}")
|
|
83
|
+
return sql.group(1).replace("```", "")
|
|
84
|
+
elif select_with:
|
|
85
|
+
self.log(
|
|
86
|
+
f"Output from LLM: {llm_response} \nExtracted SQL: {select_with.group(0)}"
|
|
87
|
+
)
|
|
88
|
+
return select_with.group(0)
|
|
89
|
+
else:
|
|
90
|
+
return llm_response
|
|
91
|
+
|
|
92
|
+
def submit_prompt(self, prompt, **kwargs) -> str:
|
|
93
|
+
self.log(
|
|
94
|
+
f"Ollama parameters:\n"
|
|
95
|
+
f"model={self.model},\n"
|
|
96
|
+
f"options={self.ollama_options},\n"
|
|
97
|
+
f"keep_alive={self.keep_alive}"
|
|
98
|
+
)
|
|
99
|
+
self.log(f"Prompt Content:\n{json.dumps(prompt, ensure_ascii=False)}")
|
|
100
|
+
response_dict = self.ollama_client.chat(
|
|
101
|
+
model=self.model,
|
|
102
|
+
messages=prompt,
|
|
103
|
+
stream=False,
|
|
104
|
+
options=self.ollama_options,
|
|
105
|
+
keep_alive=self.keep_alive,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
self.log(f"Ollama Response:\n{str(response_dict)}")
|
|
109
|
+
|
|
110
|
+
return response_dict["message"]["content"]
|