vanna 0.6.4__py3-none-any.whl → 0.6.5__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.
@@ -0,0 +1,165 @@
1
+ import qianfan
2
+
3
+ from ..base import VannaBase
4
+
5
+
6
+ class Qianfan_Chat(VannaBase):
7
+ def __init__(self, client=None, config=None):
8
+ VannaBase.__init__(self, config=config)
9
+
10
+ if "api_key" not in config:
11
+ raise Exception("Missing api_key in config")
12
+ self.api_key = config["api_key"]
13
+
14
+ if "secret_key" not in config:
15
+ raise Exception("Missing secret_key in config")
16
+ self.secret_key = config["secret_key"]
17
+
18
+ # default parameters - can be overrided using config
19
+ self.temperature = 0.9
20
+ self.max_tokens = 1024
21
+
22
+ if "temperature" in config:
23
+ self.temperature = config["temperature"]
24
+
25
+ if "max_tokens" in config:
26
+ self.max_tokens = config["max_tokens"]
27
+
28
+ self.model = config["model"] if "model" in config else "ERNIE-Speed"
29
+
30
+ if client is not None:
31
+ self.client = client
32
+ return
33
+
34
+ self.client = qianfan.ChatCompletion(ak=self.api_key,
35
+ sk=self.secret_key)
36
+
37
+ def system_message(self, message: str) -> any:
38
+ return {"role": "system", "content": message}
39
+
40
+ def user_message(self, message: str) -> any:
41
+ return {"role": "user", "content": message}
42
+
43
+ def assistant_message(self, message: str) -> any:
44
+ return {"role": "assistant", "content": message}
45
+
46
+ def get_sql_prompt(
47
+ self,
48
+ initial_prompt: str,
49
+ question: str,
50
+ question_sql_list: list,
51
+ ddl_list: list,
52
+ doc_list: list,
53
+ **kwargs,
54
+ ):
55
+ """
56
+ Example:
57
+ ```python
58
+ vn.get_sql_prompt(
59
+ question="What are the top 10 customers by sales?",
60
+ question_sql_list=[{"question": "What are the top 10 customers by sales?", "sql": "SELECT * FROM customers ORDER BY sales DESC LIMIT 10"}],
61
+ ddl_list=["CREATE TABLE customers (id INT, name TEXT, sales DECIMAL)"],
62
+ doc_list=["The customers table contains information about customers and their sales."],
63
+ )
64
+
65
+ ```
66
+
67
+ This method is used to generate a prompt for the LLM to generate SQL.
68
+
69
+ Args:
70
+ question (str): The question to generate SQL for.
71
+ question_sql_list (list): A list of questions and their corresponding SQL statements.
72
+ ddl_list (list): A list of DDL statements.
73
+ doc_list (list): A list of documentation.
74
+
75
+ Returns:
76
+ any: The prompt for the LLM to generate SQL.
77
+ """
78
+
79
+ if initial_prompt is None:
80
+ initial_prompt = f"You are a {self.dialect} expert. " + \
81
+ "Please help to generate a SQL to answer the question based on some context.Please don't give any explanation for your answer. Just only generate a SQL \n"
82
+
83
+ initial_prompt = self.add_ddl_to_prompt(
84
+ initial_prompt, ddl_list, max_tokens=self.max_tokens
85
+ )
86
+
87
+ if self.static_documentation != "":
88
+ doc_list.append(self.static_documentation)
89
+
90
+ initial_prompt = self.add_documentation_to_prompt(
91
+ initial_prompt, doc_list, max_tokens=self.max_tokens
92
+ )
93
+ message_log = []
94
+
95
+ if question_sql_list is None or len(question_sql_list) == 0:
96
+ initial_prompt = initial_prompt + f"question: {question}"
97
+ message_log.append(self.user_message(initial_prompt))
98
+ else:
99
+ for i, example in question_sql_list:
100
+ if example is None:
101
+ print("example is None")
102
+ else:
103
+ if example is not None and "question" in example and "sql" in example:
104
+ if i == 0:
105
+ initial_prompt = initial_prompt + f"question: {example['question']}"
106
+ message_log.append(self.user_message(initial_prompt))
107
+ else:
108
+ message_log.append(self.user_message(example["question"]))
109
+ message_log.append(self.assistant_message(example["sql"]))
110
+
111
+ message_log.append(self.user_message(question))
112
+ return message_log
113
+
114
+ def submit_prompt(self, prompt, **kwargs) -> str:
115
+ if prompt is None:
116
+ raise Exception("Prompt is None")
117
+
118
+ if len(prompt) == 0:
119
+ raise Exception("Prompt is empty")
120
+
121
+ # Count the number of tokens in the message log
122
+ # Use 4 as an approximation for the number of characters per token
123
+ num_tokens = 0
124
+ for message in prompt:
125
+ num_tokens += len(message["content"]) / 4
126
+
127
+ if kwargs.get("model", None) is not None:
128
+ model = kwargs.get("model", None)
129
+ print(
130
+ f"Using model {model} for {num_tokens} tokens (approx)"
131
+ )
132
+ response = self.client.do(
133
+ model=self.model,
134
+ messages=prompt,
135
+ max_output_tokens=self.max_tokens,
136
+ stop=None,
137
+ temperature=self.temperature,
138
+ )
139
+ elif self.config is not None and "model" in self.config:
140
+ print(
141
+ f"Using model {self.config['model']} for {num_tokens} tokens (approx)"
142
+ )
143
+ response = self.client.do(
144
+ model=self.config.get("model"),
145
+ messages=prompt,
146
+ max_output_tokens=self.max_tokens,
147
+ stop=None,
148
+ temperature=self.temperature,
149
+ )
150
+ else:
151
+ if num_tokens > 3500:
152
+ model = "ERNIE-Speed-128K"
153
+ else:
154
+ model = "ERNIE-Speed-8K"
155
+
156
+ print(f"Using model {model} for {num_tokens} tokens (approx)")
157
+ response = self.client.do(
158
+ model=model,
159
+ messages=prompt,
160
+ max_output_tokens=self.max_tokens,
161
+ stop=None,
162
+ temperature=self.temperature,
163
+ )
164
+
165
+ return response.body.get("result")
@@ -0,0 +1,36 @@
1
+ import qianfan
2
+
3
+ from ..base import VannaBase
4
+
5
+
6
+ class Qianfan_Embeddings(VannaBase):
7
+ def __init__(self, client=None, config=None):
8
+ VannaBase.__init__(self, config=config)
9
+
10
+ if client is not None:
11
+ self.client = client
12
+ return
13
+
14
+ if "api_key" not in config:
15
+ raise Exception("Missing api_key in config")
16
+ self.api_key = config["api_key"]
17
+
18
+ if "secret_key" not in config:
19
+ raise Exception("Missing secret_key in config")
20
+ self.secret_key = config["secret_key"]
21
+
22
+ self.client = qianfan.Embedding(ak=self.api_key, sk=self.secret_key)
23
+
24
+ def generate_embedding(self, data: str, **kwargs) -> list[float]:
25
+ if self.config is not None and "model" in self.config:
26
+ embedding = self.client.do(
27
+ model=self.config["model"],
28
+ input=[data],
29
+ )
30
+ else:
31
+ embedding = self.client.do(
32
+ model="bge-large-zh",
33
+ input=[data],
34
+ )
35
+
36
+ return embedding.get("data")[0]["embedding"]
@@ -0,0 +1,2 @@
1
+ from .Qianfan_Chat import Qianfan_Chat
2
+ from .Qianfan_embeddings import Qianfan_Embeddings
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vanna
3
- Version: 0.6.4
3
+ Version: 0.6.5
4
4
  Summary: Generate SQL queries from natural language
5
5
  Author-email: Zain Hoda <zain@vanna.ai>
6
6
  Requires-Python: >=3.9
@@ -25,6 +25,7 @@ Requires-Dist: google-cloud-bigquery ; extra == "all"
25
25
  Requires-Dist: snowflake-connector-python ; extra == "all"
26
26
  Requires-Dist: duckdb ; extra == "all"
27
27
  Requires-Dist: openai ; extra == "all"
28
+ Requires-Dist: qianfan ; extra == "all"
28
29
  Requires-Dist: mistralai ; extra == "all"
29
30
  Requires-Dist: chromadb ; extra == "all"
30
31
  Requires-Dist: anthropic ; extra == "all"
@@ -68,6 +69,7 @@ Requires-Dist: psycopg2-binary ; extra == "postgres"
68
69
  Requires-Dist: db-dtypes ; extra == "postgres"
69
70
  Requires-Dist: qdrant-client ; extra == "qdrant"
70
71
  Requires-Dist: fastembed ; extra == "qdrant"
72
+ Requires-Dist: qianfan ; extra == "qianfan"
71
73
  Requires-Dist: snowflake-connector-python ; extra == "snowflake"
72
74
  Requires-Dist: tox ; extra == "test"
73
75
  Requires-Dist: vllm ; extra == "vllm"
@@ -95,6 +97,7 @@ Provides-Extra: opensearch
95
97
  Provides-Extra: pinecone
96
98
  Provides-Extra: postgres
97
99
  Provides-Extra: qdrant
100
+ Provides-Extra: qianfan
98
101
  Provides-Extra: snowflake
99
102
  Provides-Extra: test
100
103
  Provides-Extra: vllm
@@ -43,6 +43,9 @@ vanna/pinecone/__init__.py,sha256=eO5l8aX8vKL6aIUMgAXGPt1jdqKxB_Hic6cmoVAUrD0,90
43
43
  vanna/pinecone/pinecone_vector.py,sha256=mpq1lzo3KRj2QfJEw8pwFclFQK1Oi_Nx-lDkx9Gp0mw,11448
44
44
  vanna/qdrant/__init__.py,sha256=PX_OsDOiPMvwCJ2iGER1drSdQ9AyM8iN5PEBhRb6qqY,73
45
45
  vanna/qdrant/qdrant.py,sha256=qkTWhGrVSAngJZkrcRQ8YFVHcI9j_ZoOGbF6ZVUUdsU,12567
46
+ vanna/qianfan/Qianfan_Chat.py,sha256=Z-s9MwH22T4KMR8AViAjms6qoj67pHeQkMsbK-aXf1M,5273
47
+ vanna/qianfan/Qianfan_embeddings.py,sha256=TYynAJXlyuZfmoj49h8nU6bXu_GjlXREp3tgfQUca04,954
48
+ vanna/qianfan/__init__.py,sha256=QpR43BjZQZcrcDRkyYcYiS-kyqtYmu23AHDzK0Wy1D0,90
46
49
  vanna/types/__init__.py,sha256=Qhn_YscKtJh7mFPCyCDLa2K8a4ORLMGVnPpTbv9uB2U,4957
47
50
  vanna/vannadb/__init__.py,sha256=C6UkYocmO6dmzfPKZaWojN0mI5YlZZ9VIbdcquBE58A,48
48
51
  vanna/vannadb/vannadb_vector.py,sha256=N8poMYvAojoaOF5gI4STD5pZWK9lBKPvyIjbh9dPBa0,14189
@@ -50,6 +53,6 @@ vanna/vllm/__init__.py,sha256=aNlUkF9tbURdeXAJ8ytuaaF1gYwcG3ny1MfNl_cwQYg,23
50
53
  vanna/vllm/vllm.py,sha256=oM_aA-1Chyl7T_Qc_yRKlL6oSX1etsijY9zQdjeMGMQ,2827
51
54
  vanna/weaviate/__init__.py,sha256=HL6PAl7ePBAkeG8uln-BmM7IUtWohyTPvDfcPzSGSCg,46
52
55
  vanna/weaviate/weaviate_vector.py,sha256=GEiu4Vd9w-7j10aB-zTxJ8gefqe_F-LUUGvttFs1vlg,7539
53
- vanna-0.6.4.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
54
- vanna-0.6.4.dist-info/METADATA,sha256=LqIi4Hg1y_aTEH79PX48nnY1TM-u6ese9K8Os9Cqkg0,11889
55
- vanna-0.6.4.dist-info/RECORD,,
56
+ vanna-0.6.5.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
57
+ vanna-0.6.5.dist-info/METADATA,sha256=77ggtzQplTTDdsOI0_U4k-t5UwHVmI5O3TSkpm5OXzY,11997
58
+ vanna-0.6.5.dist-info/RECORD,,
File without changes