本指南介绍如何在项目中使用 Ollama 作为 LLM 和/或 Embedding 提供商。
项目现在支持以下配置组合:
启动 Ollama 服务:
ollama serve
拉取所需的模型: ```bash
ollama pull qwen2.5:7b ollama pull deepseek-r1:7b ollama pull llama3:8b
ollama pull nomic-embed-text ```
在 app_config.py
中设置以下参数:
# 模型提供商类型
LLM_MODEL_TYPE = "ollama" # "api" 或 "ollama"
EMBEDDING_MODEL_TYPE = "ollama" # "api" 或 "ollama"
# API模式下的模型选择(Ollama模式下不使用)
API_LLM_MODEL = "qwen" # "qwen" 或 "deepseek"
# 向量数据库类型
VECTOR_DB_TYPE = "pgvector" # "chromadb" 或 "pgvector"
OLLAMA_LLM_CONFIG = {
"base_url": "http://localhost:11434", # Ollama服务地址
"model": "qwen2.5:7b", # 模型名称
"allow_llm_to_see_data": True,
"temperature": 0.7,
"n_results": 6,
"language": "Chinese",
"timeout": 60 # 超时时间(秒)
}
OLLAMA_EMBEDDING_CONFIG = {
"model_name": "nomic-embed-text", # Embedding模型名称
"base_url": "http://localhost:11434", # Ollama服务地址
"embedding_dimension": 768 # 向量维度
}
# app_config.py
LLM_MODEL_TYPE = "ollama"
EMBEDDING_MODEL_TYPE = "ollama"
VECTOR_DB_TYPE = "chromadb"
OLLAMA_LLM_CONFIG = {
"base_url": "http://localhost:11434",
"model": "qwen2.5:7b",
"temperature": 0.7,
"timeout": 60
}
OLLAMA_EMBEDDING_CONFIG = {
"model_name": "nomic-embed-text",
"base_url": "http://localhost:11434",
"embedding_dimension": 768
}
# app_config.py
LLM_MODEL_TYPE = "api"
EMBEDDING_MODEL_TYPE = "ollama"
API_LLM_MODEL = "qwen"
VECTOR_DB_TYPE = "pgvector"
# 使用现有的 API_QWEN_CONFIG
# 使用 OLLAMA_EMBEDDING_CONFIG
# app_config.py
LLM_MODEL_TYPE = "ollama"
EMBEDDING_MODEL_TYPE = "api"
VECTOR_DB_TYPE = "chromadb"
# 使用 OLLAMA_LLM_CONFIG
# 使用现有的 API_EMBEDDING_CONFIG
from common.utils import (
is_using_ollama_llm,
is_using_ollama_embedding,
get_current_model_info,
print_current_config
)
# 检查当前配置
print_current_config()
# 检查是否使用Ollama
if is_using_ollama_llm():
print("当前使用Ollama LLM")
if is_using_ollama_embedding():
print("当前使用Ollama Embedding")
# 获取模型信息
model_info = get_current_model_info()
print(model_info)
from vanna_llm_factory import create_vanna_instance
# 根据配置自动创建合适的实例
vn = create_vanna_instance()
# 使用实例
sql = vn.generate_sql("查询所有用户的信息")
print(sql)
# 直接使用Ollama LLM
from customollama.ollama_chat import OllamaChat
config = {
"base_url": "http://localhost:11434",
"model": "qwen2.5:7b",
"temperature": 0.7
}
ollama_llm = OllamaChat(config=config)
response = ollama_llm.chat_with_llm("你好")
# 直接使用Ollama Embedding
from customollama.ollama_embedding import OllamaEmbeddingFunction
embedding_func = OllamaEmbeddingFunction(
model_name="nomic-embed-text",
base_url="http://localhost:11434",
embedding_dimension=768
)
embeddings = embedding_func(["文本1", "文本2"])
python test_config_utils.py
python test_ollama_integration.py
# 测试Ollama LLM连接
from customollama.ollama_chat import OllamaChat
config = {"base_url": "http://localhost:11434", "model": "qwen2.5:7b"}
ollama_llm = OllamaChat(config=config)
result = ollama_llm.test_connection()
print(result)
# 测试Ollama Embedding连接
from customollama.ollama_embedding import OllamaEmbeddingFunction
embedding_func = OllamaEmbeddingFunction(
model_name="nomic-embed-text",
base_url="http://localhost:11434",
embedding_dimension=768
)
result = embedding_func.test_connection()
print(result)
问题:Ollama API调用失败: Connection refused
解决方案:
ollama serve
http://localhost:11434
)问题:model 'qwen2.5:7b' not found
解决方案:
ollama pull qwen2.5:7b
ollama list
问题:向量维度不匹配: 期望 768, 实际 384
解决方案:
embedding_dimension
为实际维度问题:Ollama API调用超时
解决方案:
timeout
配置值qwen2.5:7b
, llama3:8b
- 适合资源有限的环境qwen2.5:14b
, deepseek-r1:32b
- 适合性能要求高的场景# 针对性能优化的配置
OLLAMA_LLM_CONFIG = {
"base_url": "http://localhost:11434",
"model": "qwen2.5:7b",
"temperature": 0.1, # 降低随机性,提高一致性
"timeout": 120, # 增加超时时间
}
项目采用了统一的组合类管理方式,所有LLM与向量数据库的组合都在 common/vanna_combinations.py
中定义:
# 可用的组合类
from common.vanna_combinations import (
Vanna_Qwen_ChromaDB,
Vanna_DeepSeek_ChromaDB,
Vanna_Ollama_ChromaDB,
Vanna_Qwen_PGVector,
Vanna_DeepSeek_PGVector,
Vanna_Ollama_PGVector,
get_vanna_class,
print_available_combinations
)
# 动态获取组合类
cls = get_vanna_class("ollama", "chromadb") # 返回 Vanna_Ollama_ChromaDB
# 查看所有可用组合
print_available_combinations()
vanna_llm_factory.py
使用统一的组合类文件,自动根据配置选择合适的组合:
from vanna_llm_factory import create_vanna_instance
# 根据 app_config.py 中的配置自动创建实例
vn = create_vanna_instance()
python test_config_utils.py
python test_ollama_integration.py
python test_vanna_combinations.py