当前位置: 首页 > wzjs >正文

西安代做毕业设计网站企业网站的基本功能

西安代做毕业设计网站,企业网站的基本功能,做网站销售怎么样,网站美化的目标这篇文章介绍如何将LLM RAG Vanna结合起来,在一个项目中通过和LLM对话的方式查询数据库中的记录,并显示结果。而不需要人为的手动的通过执行SQL的方式来查询数据库记录。 OLLAMA Install Ollama curl -fsSL https://ollama.com/install.sh | sh Not…

这篇文章介绍如何将LLM + RAG + Vanna结合起来,在一个项目中通过和LLM对话的方式查询数据库中的记录,并显示结果。而不需要人为的手动的通过执行SQL的方式来查询数据库记录。


OLLAMA

Install Ollama

curl -fsSL https://ollama.com/install.sh | sh

Note: The default location to store models file is: /usr/share/ollama/.ollama/models, to change the location, refer to: https://github.com/ollama/ollama/blob/main/docs/faq.md#where-are-models-stored

Query Libraries:
library

Config proxy

sudo vi /etc/systemd/system/ollama.service

如果需要,增加http_proxy和https_proxy,

[Unit]
Description=Ollama Service
After=network-online.target[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="PATH=/home/sunny/bin:/home/sunny/conda/condabin:/home/sunny/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/sunny/yunjiang/tools/apache-maven-3.9.9/bin:/home/sunny/conda/bin:/home/sunny/conda/bin"Environment="http_proxy=http://child-prc.intel.com:913"
Environment="https_proxy=http://child-prc.intel.com:913"[Install]
WantedBy=default.target

重启服务,

sudo systemctl daemon-reload

sudo systemctl restart ollama

Pull model

ollama pull llama3.2

List model

ollama list

Run model

ollama run llama3.2

Ask Question via Restful API

curl http://localhost:11434/api/generate -d '{ "model": "llama3.2", "prompt": "How are you today?", "stream": false}'

Install CrhomaDB

Refer to: https://blog.csdn.net/funnyrand/article/details/148132003

Install Vanna

Install Vanna

pip3 install vanna

For more details of Vanna, refer to:

Vanna.AI Documentation

https://github.com/vanna-ai/vanna?tab=readme-ov-file

Install ClickHouse Connect

pip3 install clickhouse_connect

Install MySQL Connect

pip3 install PyMySQL

Install ClickHouse

Refer to: ClickHouse总结_checkhouse数据库-CSDN博客

注:需要插入真实数据到ClickHouse,下面的部分仅做演示。

Python code to use Vanna

ntr_test1.py

from vanna.ollama import Ollama
from vanna.chromadb import ChromaDB_VectorStoreclass MyVanna(ChromaDB_VectorStore, Ollama):def __init__(self, config=None):ChromaDB_VectorStore.__init__(self, config=config)Ollama.__init__(self, config=config)def train():# The information schema query may need some tweaking depending on your database. This is a good starting point.df_information_schema = vn.run_sql("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'ntr'")# This will break up the information schema into bite-sized chunks that can be referenced by the LLMplan = vn.get_training_plan_generic(df_information_schema)#print(plan)# If you like the plan, then uncomment this and run it to train#vn.train(plan=plan)# The following are methods for adding training data. Make sure you modify the examples to match your database.# DDL statements are powerful because they specify table names, colume names, types, and potentially relationshipsvn.train(ddl="""CREATE TABLE ntr.metadata (timestamp DateTime64(6),srcMac FixedString(17),destMac FixedString(17),srcIp IPv4,destIp IPv4,srcPort UInt16,destPort UInt16,etherType UInt16,ipProto UInt16,packetSize UInt16,offset UInt64,pcapFile String)ENGINE = MergeTree()PARTITION BY toStartOfHour(timestamp)ORDER BY (timestamp, srcIp, destIp, srcPort, destPort, etherType, ipProto);""")vn.train(ddl="""CREATE TABLE ntr.metadata_stats_all (timestamp DateTime,throughput Float64,packetCount UInt64,packetSizes UInt64)ENGINE = MergeTree()PARTITION BY toYYYYMMDD(timestamp)ORDER BY (timestamp);""")vn.train(ddl="""CREATE TABLE ntr.global_config (key String,value String,initVal String,regexp String) ENGINE = MergeTree()PARTITION BY (key)ORDER BY (key);""")# Sometimes you may want to add documentation about your business terminology or definitions.# vn.train(documentation="Our business defines OTIF score as the percentage of orders that are delivered on time and in full")# You can also add SQL queries to your training data. This is useful if you have some queries already laying around. You can just copy and paste those from your editor to begin generating new SQL.#vn.train(sql="select * from mydb.person where name='sunny';")# At any time you can inspect what training data the package is able to referencetraining_data = vn.get_training_data()training_data# Main
vn = MyVanna(config={'model': 'llama3.2'})vn.connect_to_clickhouse(host='172.17.28.100', dbname='ntr', user="default", password="changeme", port=8123)#train()# You can remove training data if there's obsolete/incorrect information. 
# vn.remove_training_data(id='1-ddl')## Asking the AI
# Whenever you ask a new question, it will find the 10 most relevant pieces of training data and use it as part of the LLM prompt to generate the SQL.# vn.ask(question="How many metadata records with src IP address 117.35.136.101 in ntr?", allow_llm_to_see_data=True)
# vn.ask(question="Query metadata which src IP address is 117.35.136.101.")
# rt = vn.ask(question="How many items in table ntr.metadata witch src IP address 117.35.136.101?", visualize=True, allow_llm_to_see_data=True)
rt = vn.ask(question="Query items in ntr.metadata table which src IP address is 117.35.136.101.", visualize=False, allow_llm_to_see_data=True)
#print(rt)

运行结果:

sunny@r05u33-nex-wvie-spr-cd:~/work/llm$  /usr/bin/env /bin/python3 /home/sunny/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 39887 -- /home/sunny/work/llm/vanna/ntr/ntr_test1.py 
<clickhouse_connect.driver.httpclient.HttpClient object at 0x7f1b477f29e0>
SQL Prompt: [{'role': 'system', 'content': "You are a SQL expert. Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. \n===Tables \n\n        CREATE TABLE ntr.metadata (\n            timestamp DateTime64(6),\n            srcMac FixedString(17),\n            destMac FixedString(17),\n            srcIp IPv4,\n            destIp IPv4,\n            srcPort UInt16,\n            destPort UInt16,\n            etherType UInt16,\n            ipProto UInt16,\n            packetSize UInt16,\n            offset UInt64,\n            pcapFile String\n        )\n        ENGINE = MergeTree()\n        PARTITION BY toStartOfHour(timestamp)\n        ORDER BY (\n            timestamp, \n            srcIp, \n            destIp, \n            srcPort, \n            destPort, \n            etherType, \n            ipProto\n        );\n    \n\n\n        CREATE TABLE ntr.metadata_stats_all (\n            timestamp DateTime,\n            throughput Float64,\n            packetCount UInt64,\n            packetSizes UInt64\n        )\n        ENGINE = MergeTree()\n        PARTITION BY toYYYYMMDD(timestamp)\n        ORDER BY (timestamp);\n    \n\n\n        CREATE TABLE ntr.global_config (\n            key String,\n            value String,\n            initVal String,\n            regexp String\n        ) \n        ENGINE = MergeTree()\n        PARTITION BY (key)\n        ORDER BY (key);\n    \n\n===Response Guidelines \n1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \n2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \n3. If the provided context is insufficient, please explain why it can't be generated. \n4. Please use the most relevant table(s). \n5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \n6. Ensure that the output SQL is SQL-compliant and executable, and free of syntax errors. \n"}, {'role': 'user', 'content': 'Query items in ntr.metadata table which src IP address is 117.35.136.101.'}, {'role': 'assistant', 'content': "SELECT * \nFROM ntr.metadata\nWHERE srcIp = '117.35.136.101'"}, {'role': 'user', 'content': 'Query metadata which src port is 61446'}, {'role': 'assistant', 'content': 'SELECT * FROM ntr.metadata WHERE srcPort = 61446'}, {'role': 'user', 'content': 'Query metadata which src port is 61446'}, {'role': 'assistant', 'content': 'SELECT DISTINCT srcPort\nFROM ntr.metadata\nWHERE srcPort = 61446'}, {'role': 'user', 'content': 'How many items in table ntr.metadata witch src IP address 117.35.136.101?'}, {'role': 'assistant', 'content': "SELECT COUNT(*) \nFROM ntr.metadata\nWHERE srcIp = '117.35.136.101'"}, {'role': 'user', 'content': 'How many items in table ntr.metadata?'}, {'role': 'assistant', 'content': 'SELECT COUNT(*) \nFROM ntr.metadata'}, {'role': 'user', 'content': 'Query items in ntr.metadata table which src IP address is 117.35.136.101.'}]
Info: Ollama parameters:
model=llama3.2:latest,
options={},
keep_alive=None
Info: Prompt Content:
[{"role": "system", "content": "You are a SQL expert. Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. \n===Tables \n\n        CREATE TABLE ntr.metadata (\n            timestamp DateTime64(6),\n            srcMac FixedString(17),\n            destMac FixedString(17),\n            srcIp IPv4,\n            destIp IPv4,\n            srcPort UInt16,\n            destPort UInt16,\n            etherType UInt16,\n            ipProto UInt16,\n            packetSize UInt16,\n            offset UInt64,\n            pcapFile String\n        )\n        ENGINE = MergeTree()\n        PARTITION BY toStartOfHour(timestamp)\n        ORDER BY (\n            timestamp, \n            srcIp, \n            destIp, \n            srcPort, \n            destPort, \n            etherType, \n            ipProto\n        );\n    \n\n\n        CREATE TABLE ntr.metadata_stats_all (\n            timestamp DateTime,\n            throughput Float64,\n            packetCount UInt64,\n            packetSizes UInt64\n        )\n        ENGINE = MergeTree()\n        PARTITION BY toYYYYMMDD(timestamp)\n        ORDER BY (timestamp);\n    \n\n\n        CREATE TABLE ntr.global_config (\n            key String,\n            value String,\n            initVal String,\n            regexp String\n        ) \n        ENGINE = MergeTree()\n        PARTITION BY (key)\n        ORDER BY (key);\n    \n\n===Response Guidelines \n1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \n2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \n3. If the provided context is insufficient, please explain why it can't be generated. \n4. Please use the most relevant table(s). \n5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \n6. Ensure that the output SQL is SQL-compliant and executable, and free of syntax errors. \n"}, {"role": "user", "content": "Query items in ntr.metadata table which src IP address is 117.35.136.101."}, {"role": "assistant", "content": "SELECT * \nFROM ntr.metadata\nWHERE srcIp = '117.35.136.101'"}, {"role": "user", "content": "Query metadata which src port is 61446"}, {"role": "assistant", "content": "SELECT * FROM ntr.metadata WHERE srcPort = 61446"}, {"role": "user", "content": "Query metadata which src port is 61446"}, {"role": "assistant", "content": "SELECT DISTINCT srcPort\nFROM ntr.metadata\nWHERE srcPort = 61446"}, {"role": "user", "content": "How many items in table ntr.metadata witch src IP address 117.35.136.101?"}, {"role": "assistant", "content": "SELECT COUNT(*) \nFROM ntr.metadata\nWHERE srcIp = '117.35.136.101'"}, {"role": "user", "content": "How many items in table ntr.metadata?"}, {"role": "assistant", "content": "SELECT COUNT(*) \nFROM ntr.metadata"}, {"role": "user", "content": "Query items in ntr.metadata table which src IP address is 117.35.136.101."}]
Info: Ollama Response:
model='llama3.2:latest' created_at='2025-05-30T08:21:59.889943887Z' done=True done_reason='stop' total_duration=8834583846 load_duration=6108105509 prompt_eval_count=679 prompt_eval_duration=2053141142 eval_count=22 eval_duration=651663457 message=Message(role='assistant', content="SELECT * \nFROM ntr.metadata\nWHERE srcIp = '117.35.136.101'", images=None, tool_calls=None)
LLM Response: SELECT * 
FROM ntr.metadata
WHERE srcIp = '117.35.136.101'
SELECT * 
FROM ntr.metadata
WHERE srcIp = '117.35.136.101'timestamp                srcMac               destMac           srcIp  ... ipProto packetSize       offset                      pcapFile
0 2025-05-23 14:56:35.953557+08:00  b'e5:19:63:7b:de:b9'  b'55:61:5b:5e:74:de'  117.35.136.101  ...      17       1514  56228199546  output_0_4_5_1747983214.pcap[1 rows x 14 columns]

可以看到,其正确的输出了查询结果。

http://www.dtcms.com/wzjs/483051.html

相关文章:

  • 做网站要学编程麽线上营销推广的公司
  • 做垃圾网站可行吗美食软文300范例
  • 套用别人产品图片做网站外贸网络推广
  • 免费个人网站模板下载360收录
  • 成都网站建设冠辰北京seo关键词排名优化
  • 电子商务网站建设与管理课程心得百度sem是什么意思
  • 受欢迎的网站建设平台自己搭建一个网站
  • 四川哪家网站推广做的好什么是指数基金
  • 做的网站如何全屏代码百度投诉电话人工服务总部
  • jsp网站开发简单代码seo销售
  • 顺德定制网站建设seoul怎么读
  • 休闲小零食网站开发方案网络营销主要做些什么工作
  • 共青团员建设网站百度小说搜索排行榜
  • 政府网站建设发展思路关键词优化推广公司排名
  • 网站seo排名优化软件深圳seo公司助力网络营销飞跃
  • 恩做网站动态页面好合肥seo排名扣费
  • 网站开发是用什么语言长沙市云网站建设
  • 企业网站 asp.net全网网站快速排名推广软件
  • 如何管理手机网站首页最新搜索关键词
  • 网页开发用什么编程语言专业搜索引擎seo服务商
  • 简述网站开发基本流程杭州搜索引擎优化公司
  • 海珠做网站公司正规百度推广
  • 义乌网站建设多少钱软文广告图片
  • 建网站网站新能源汽车公司
  • 做文交所的都有哪些网站5118站长工具箱
  • b2c商城网站哪个行业最需要推广
  • 乐陵网站开发小吃培训机构排名前十
  • 男女做啊免费视频网站搜索引擎优化实训
  • 接视频做的网网站百度seo 优化
  • 网站规划与建设课程设计临沂百度推广多少钱