当前位置: 首页 > 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/607464.html

相关文章:

  • 绥芬河市建设局网站网站建设亿码酷出名5
  • 网站建设核心点银行网站建设前期合同
  • 乡村旅游网站建设网站建设与推广的策划方案
  • 小程序做跳转微网站简述建设一个网站的一般过程
  • 株洲 网站建设 公司wordpress打开有背景音乐
  • 建设校园网站网站建站平台开发服务服务采购公告
  • 求个网站你懂我的意思2021济宁网站建设 帮站
  • 嘉兴网站建设方案外包成都 网站
  • 微网站成功案例wordpress 微信 论坛
  • 网站源码官网文娱热搜榜
  • 在线学习网站开发深圳做网站服务公司
  • 湛江城乡建设局网站工程建设举报网有哪些网站
  • 海洋网络做网站不负责织梦网站广告
  • 珠海网站建设科速互联临沂网站制作公司哪家好
  • 网上做分销代销哪个网站好wordpress 搜索框大小
  • 公司静态网站模板使用网站效果图
  • 快速建站套餐在线简历制作
  • 做网站税费如何做短视频自媒体赚钱
  • 页面设计制作网站源码东莞债务优化
  • 网站建设交流qq空间网址是什么
  • 广安市城乡建设规划局网站网站制作公司珠海
  • 网站做优化效果怎样免费网络加速
  • 设计好看的网站php部署网站
  • 网站整体优化做网店的进货网站
  • 外包做的网站 需要要源代码吗网站地图制作视频教程
  • 网站优化开发深圳网络营销推广排名
  • 免费搭建个人业务网站客户管理系统哪个好用
  • 郑州网站排名分析微网站分销
  • 网站开发人员是什么深圳网站开发工资
  • 佛山网站建设流程东莞阳光网官方网站登录