From d40c0dd000d230caac438f5b3384d3c45d5046a2 Mon Sep 17 00:00:00 2001 From: Ivan Lee <84584280+ivanleekk@users.noreply.github.com> Date: Wed, 20 Aug 2025 00:56:12 +0800 Subject: [PATCH] fix: llamacpp --- .python-version | 2 +- main.py | 4 +-- tradingagents/agents/utils/memory.py | 47 +++++++++++++++++++++++----- tradingagents/default_config.py | 2 +- tradingagents/graph/trading_graph.py | 4 ++- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/.python-version b/.python-version index fdcfcfdf..7c7a975f 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.12 \ No newline at end of file +3.10 \ No newline at end of file diff --git a/main.py b/main.py index 057781cd..84c90366 100644 --- a/main.py +++ b/main.py @@ -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 diff --git a/tradingagents/agents/utils/memory.py b/tradingagents/agents/utils/memory.py index 69b8ab8c..3751ee4f 100644 --- a/tradingagents/agents/utils/memory.py +++ b/tradingagents/agents/utils/memory.py @@ -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)""" diff --git a/tradingagents/default_config.py b/tradingagents/default_config.py index 921f25d4..a21dd8fb 100644 --- a/tradingagents/default_config.py +++ b/tradingagents/default_config.py @@ -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, diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py index b3b00a72..38fef3f5 100644 --- a/tradingagents/graph/trading_graph.py +++ b/tradingagents/graph/trading_graph.py @@ -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"]