Spaces:
Sleeping
Sleeping
Commit ·
70db80b
1
Parent(s): 2362139
fix: change ollama to api
Browse files- Dockerfile +1 -11
- docker-compose.yaml +0 -44
- entrypoint.sh +0 -9
- requirements.txt +0 -1
- src/FinancialAgent.py +1 -3
- src/FinancialAgentOllama.py +24 -16
- src/OllamaAPI.py +0 -0
Dockerfile
CHANGED
|
@@ -8,23 +8,13 @@ RUN apt-get update && apt-get install -y \
|
|
| 8 |
git \
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
RUN curl -fsSL https://ollama.com/install.sh | sh
|
| 12 |
-
|
| 13 |
COPY requirements.txt ./
|
| 14 |
RUN pip3 install --no-cache-dir -r requirements.txt
|
| 15 |
|
| 16 |
-
ENV OLLAMA_HOME=/app/.ollama
|
| 17 |
-
ENV HF_HOME=/app/.cache/huggingface
|
| 18 |
-
|
| 19 |
-
RUN mkdir -p $OLLAMA_HOME $HF_HOME
|
| 20 |
-
RUN chmod 777 $OLLAMA_HOME $HF_HOME
|
| 21 |
-
|
| 22 |
COPY src/ ./src/
|
| 23 |
|
| 24 |
EXPOSE 8501
|
| 25 |
-
EXPOSE 11434
|
| 26 |
|
| 27 |
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health || exit 1
|
| 28 |
|
| 29 |
-
CMD
|
| 30 |
-
streamlit run src/streamlit_app.py --server.port=8501 --server.address=0.0.0.0
|
|
|
|
| 8 |
git \
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
|
|
|
|
|
|
| 11 |
COPY requirements.txt ./
|
| 12 |
RUN pip3 install --no-cache-dir -r requirements.txt
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
COPY src/ ./src/
|
| 15 |
|
| 16 |
EXPOSE 8501
|
|
|
|
| 17 |
|
| 18 |
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health || exit 1
|
| 19 |
|
| 20 |
+
CMD ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
docker-compose.yaml
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
version: "3.9"
|
| 2 |
-
|
| 3 |
-
services:
|
| 4 |
-
ollama:
|
| 5 |
-
build:
|
| 6 |
-
context: .
|
| 7 |
-
dockerfile: Dockerfile
|
| 8 |
-
command: >
|
| 9 |
-
sh -c "
|
| 10 |
-
ollama serve &
|
| 11 |
-
sleep 3 &&
|
| 12 |
-
ollama pull qwen3:4b &&
|
| 13 |
-
tail -f /dev/null
|
| 14 |
-
"
|
| 15 |
-
volumes:
|
| 16 |
-
- ollama-models:/root/.ollama
|
| 17 |
-
ports:
|
| 18 |
-
- "11434:11434" # Ollama server port
|
| 19 |
-
healthcheck:
|
| 20 |
-
test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
|
| 21 |
-
interval: 10s
|
| 22 |
-
timeout: 5s
|
| 23 |
-
retries: 5
|
| 24 |
-
|
| 25 |
-
streamlit:
|
| 26 |
-
build:
|
| 27 |
-
context: .
|
| 28 |
-
dockerfile: Dockerfile
|
| 29 |
-
command: >
|
| 30 |
-
streamlit run src/streamlit_app.py
|
| 31 |
-
--server.port=8501
|
| 32 |
-
--server.address=0.0.0.0
|
| 33 |
-
ports:
|
| 34 |
-
- "8501:8501"
|
| 35 |
-
depends_on:
|
| 36 |
-
ollama:
|
| 37 |
-
condition: service_healthy
|
| 38 |
-
environment:
|
| 39 |
-
OLLAMA_HOST: http://ollama:11434
|
| 40 |
-
volumes:
|
| 41 |
-
- .:/app
|
| 42 |
-
|
| 43 |
-
volumes:
|
| 44 |
-
ollama-models:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
entrypoint.sh
DELETED
|
@@ -1,9 +0,0 @@
|
|
| 1 |
-
ollama serve &
|
| 2 |
-
echo "serving ollama"
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
echo "sleeping for 5 sec"
|
| 6 |
-
sleep 5
|
| 7 |
-
|
| 8 |
-
echo "start steamlit"
|
| 9 |
-
streamlit run src/streamlit_app.py --server.port=8501 --server.address=0.0.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
ollama
|
| 2 |
pandas
|
| 3 |
streamlit
|
| 4 |
matplotlib
|
|
|
|
|
|
|
| 1 |
pandas
|
| 2 |
streamlit
|
| 3 |
matplotlib
|
src/FinancialAgent.py
CHANGED
|
@@ -20,11 +20,9 @@ class FinancialAgentFactory(ABC):
|
|
| 20 |
self.st.session_state.messages = []
|
| 21 |
self.st.session_state["openai_model"] = self.model_name
|
| 22 |
|
| 23 |
-
@abstractmethod
|
| 24 |
def render_header(self, header="Financial Agent"):
|
| 25 |
self.st.title(header)
|
| 26 |
-
|
| 27 |
-
@abstractmethod
|
| 28 |
def render_messages(self):
|
| 29 |
"""Render previous chat messages."""
|
| 30 |
for message in self.st.session_state.messages:
|
|
|
|
| 20 |
self.st.session_state.messages = []
|
| 21 |
self.st.session_state["openai_model"] = self.model_name
|
| 22 |
|
|
|
|
| 23 |
def render_header(self, header="Financial Agent"):
|
| 24 |
self.st.title(header)
|
| 25 |
+
|
|
|
|
| 26 |
def render_messages(self):
|
| 27 |
"""Render previous chat messages."""
|
| 28 |
for message in self.st.session_state.messages:
|
src/FinancialAgentOllama.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
from FinancialAgent import FinancialAgentFactory
|
| 3 |
from prompt import REFINERY_PROMPT
|
| 4 |
from models import ResponseState
|
|
@@ -8,24 +8,25 @@ import streamlit as st
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
class FinancialAgentOllama(FinancialAgentFactory):
|
| 13 |
"""Concrete Financial Agent using Ollama."""
|
| 14 |
-
def __init__(self, st, model_name="
|
| 15 |
-
pull(model_name)
|
| 16 |
super().__init__(st, model_name)
|
| 17 |
-
self.client =
|
| 18 |
self.vector_db = FAISS.load_local("vs_68bf713eea2c81919ac08298a05d6704", embedding, allow_dangerous_deserialization=True)
|
| 19 |
|
| 20 |
def __stream_answer__(self, instructions, input_messages):
|
| 21 |
-
response_stream = self.client(
|
| 22 |
-
message=input_messages + [{"role": "user", "content": instructions}],
|
| 23 |
model=self.model_name,
|
|
|
|
| 24 |
stream=True
|
| 25 |
)
|
| 26 |
-
|
| 27 |
for chunk in response_stream:
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def generate_final_answer(self, context_prompt):
|
| 31 |
"""Generate final answer using context."""
|
|
@@ -68,20 +69,27 @@ class FinancialAgentOllama(FinancialAgentFactory):
|
|
| 68 |
self.st.markdown(prompt)
|
| 69 |
|
| 70 |
# Step 1: Run refinery prompt
|
| 71 |
-
response = self.client(
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
response_format=ResponseState.model_json_schema(),
|
| 75 |
df_head=self.df.head().to_markdown(),
|
| 76 |
df_columns=self.df.columns.tolist(),
|
| 77 |
df_sample=self.df.sample(5).to_markdown()
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
format=ResponseState
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
-
response_state: ResponseState = ResponseState.model_validate_json(
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Step 2: Check if context is needed
|
| 87 |
if response_state.isNeedContext:
|
|
|
|
| 1 |
+
|
| 2 |
from FinancialAgent import FinancialAgentFactory
|
| 3 |
from prompt import REFINERY_PROMPT
|
| 4 |
from models import ResponseState
|
|
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
|
| 11 |
+
from OllamaAPI import OllamaAPIClient
|
| 12 |
|
| 13 |
class FinancialAgentOllama(FinancialAgentFactory):
|
| 14 |
"""Concrete Financial Agent using Ollama."""
|
| 15 |
+
def __init__(self, st, model_name="qwen3:4b", url="https://mrfirdauss-ollama-api.hf.space", embedding=None):
|
|
|
|
| 16 |
super().__init__(st, model_name)
|
| 17 |
+
self.client = OllamaAPIClient(url)
|
| 18 |
self.vector_db = FAISS.load_local("vs_68bf713eea2c81919ac08298a05d6704", embedding, allow_dangerous_deserialization=True)
|
| 19 |
|
| 20 |
def __stream_answer__(self, instructions, input_messages):
|
| 21 |
+
response_stream = self.client.chat(
|
|
|
|
| 22 |
model=self.model_name,
|
| 23 |
+
messages=input_messages + [{"role": "user", "content": instructions}],
|
| 24 |
stream=True
|
| 25 |
)
|
|
|
|
| 26 |
for chunk in response_stream:
|
| 27 |
+
if "message" in chunk and "content" in chunk["message"]:
|
| 28 |
+
yield chunk["message"]["content"]
|
| 29 |
+
|
| 30 |
|
| 31 |
def generate_final_answer(self, context_prompt):
|
| 32 |
"""Generate final answer using context."""
|
|
|
|
| 69 |
self.st.markdown(prompt)
|
| 70 |
|
| 71 |
# Step 1: Run refinery prompt
|
| 72 |
+
response = self.client.chat(
|
| 73 |
+
model=self.model_name,
|
| 74 |
+
messages=[{"role": m["role"], "content": m["content"]}
|
| 75 |
+
for m in self.st.session_state.messages] + [
|
| 76 |
+
{
|
| 77 |
+
"role": "user",
|
| 78 |
+
"content": REFINERY_PROMPT.format(
|
| 79 |
response_format=ResponseState.model_json_schema(),
|
| 80 |
df_head=self.df.head().to_markdown(),
|
| 81 |
df_columns=self.df.columns.tolist(),
|
| 82 |
df_sample=self.df.sample(5).to_markdown()
|
| 83 |
+
)
|
| 84 |
+
}
|
| 85 |
+
],
|
| 86 |
+
format= ResponseState,
|
| 87 |
+
stream=False
|
| 88 |
)
|
| 89 |
|
| 90 |
+
response_state: ResponseState = ResponseState.model_validate_json(
|
| 91 |
+
response["message"]["content"]
|
| 92 |
+
)
|
| 93 |
|
| 94 |
# Step 2: Check if context is needed
|
| 95 |
if response_state.isNeedContext:
|
src/OllamaAPI.py
ADDED
|
File without changes
|