fix: llamacpp

This commit is contained in:
Ivan Lee 2025-08-20 00:56:12 +08:00
parent 2013221499
commit d40c0dd000
5 changed files with 47 additions and 12 deletions

View File

@ -1 +1 @@
3.12
3.10

View File

@ -8,7 +8,7 @@ load_dotenv()
# Create a custom config
config = DEFAULT_CONFIG.copy()
config["llm_provider"] = "llamacpp" # Use a different model
config["backend_url"] = "http://localhost:8000/v1" # Use a different backend
config["backend_url"] = "http://localhost:8080/v1" # Use a different backend
config["deep_think_llm"] = (
"models/Qwen3-4B-Thinking-2507-UD-Q8_K_XL.gguf" # Use a different model
)
@ -22,7 +22,7 @@ config["online_tools"] = True # Increase debate rounds
ta = TradingAgentsGraph(debug=True, config=config)
# forward propagate
_, decision = ta.propagate("SPY", "2024-07-09")
_, decision = ta.propagate("AAPL", "2025-08-18")
print(decision)
# Memorize mistakes and reflect

View File

@ -5,8 +5,38 @@ from openai import OpenAI
class FinancialSituationMemory:
def __init__(self, name, config):
if config["backend_url"] == "http://localhost:11434/v1":
if config["llm_provider"] == "ollama":
self.embedding = "nomic-embed-text"
elif config["llm_provider"] == "llamacpp":
try:
from langchain_community.embeddings.llamacpp import LlamaCppEmbeddings
except Exception:
LlamaCppEmbeddings = None
# path to your local gguf/ggml model file (absolute recommended)
self.embedding = config.get(
"embedding_model_path", "models/nomic-embed-text-v2-moe.f32.gguf"
)
if LlamaCppEmbeddings is None:
raise RuntimeError(
"LlamaCppEmbeddings not available; install llama-cpp-python and langchain_community."
)
# instantiate the LlamaCpp embeddings wrapper
self.embeddings_model = LlamaCppEmbeddings(
model_path=self.embedding,
n_ctx=2048,
n_parts=-1,
seed=0,
f16_kv=True,
logits_all=False,
vocab_only=False,
use_mlock=False,
n_threads=4,
n_batch=512,
n_gpu_layers=0,
verbose=False,
device="cpu",
)
self.client = None
else:
self.embedding = "text-embedding-3-small"
self.client = OpenAI(base_url=config["backend_url"])
@ -14,12 +44,15 @@ class FinancialSituationMemory:
self.situation_collection = self.chroma_client.create_collection(name=name)
def get_embedding(self, text):
"""Get OpenAI embedding for a text"""
response = self.client.embeddings.create(
model=self.embedding, input=text
)
return response.data[0].embedding
"""Get embedding for a text (provider-dependent)"""
if self.embeddings_model is not None:
# LlamaCppEmbeddings implements embed_documents(list[str]) -> list[list[float]]
emb = self.embeddings_model.embed_documents([text])
return emb[0]
else:
# OpenAI/ollama path using OpenAI client
response = self.client.embeddings.create(model=self.embedding, input=text)
return response.data[0].embedding
def add_situations(self, situations_and_advice):
"""Add financial situations and their corresponding advice. Parameter is a list of tuples (situation, rec)"""

View File

@ -12,7 +12,7 @@ DEFAULT_CONFIG = {
"llm_provider": "ollama",
"deep_think_llm": "llama3.2",
"quick_think_llm": "llama3.2",
"backend_url": "http://localhost:11434/v1",
"backend_url": "http://localhost:11434",
# Debate and discussion settings
"max_debate_rounds": 1,
"max_risk_discuss_rounds": 1,

View File

@ -77,7 +77,9 @@ class TradingAgentsGraph:
)
elif self.config["llm_provider"].lower() == "ollama":
self.deep_thinking_llm = ChatOllama(model=self.config["deep_think_llm"])
self.quick_thinking_llm = ChatOllama(model=self.config["quick_think_llm"])
self.quick_thinking_llm = ChatOllama(
model=self.config["quick_think_llm"],
)
elif self.config["llm_provider"].lower() == "anthropic":
self.deep_thinking_llm = ChatAnthropic(
model=self.config["deep_think_llm"], base_url=self.config["backend_url"]