Skip to content

Cleanup configs #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dockers/llm.rag.service/.env-local
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
WEAVIATE_URI_WITH_PORT=localhost:8080
WEAVIATE_GRPC_URI_WITH_PORT=localhost:50051
WEAVIATE_INDEX_NAME=my_custom_index

MODEL_LLM_SERVER_URL=http://localhost:9000/v1

SQL_SEARCH_DB_AND_MODEL_PATH=/tmp/db/

RAGLLM_LOGS_PATH=logs/ragllm.log
2 changes: 2 additions & 0 deletions dockers/llm.rag.service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
uv.lock
1 change: 1 addition & 0 deletions dockers/llm.rag.service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ COPY serveragllm.py .
COPY logging_config.py .
COPY serverragllm_csv_to_weaviate_local.py .
COPY common.py .
COPY config.py .
COPY pyproject.toml .

EXPOSE 8000
Expand Down
19 changes: 8 additions & 11 deletions dockers/llm.rag.service/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,13 @@ def get_answer_with_settings_with_weaviate_filter(
question,
vectorstore,
client,
model_id,
max_tokens,
model_temperature,
system_prompt,
relevant_docs,
llm_server_url,
sql_search_db_and_model_path,
alpha,
Copy link

@selvik selvik Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we able to replace alpha with weaviate settings here? And also use the sql_search_settings for the other parameters?

max_context_length,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_context_length could also be added to llm_settings because it defines how much of the total possible context length of the LLM we will be using.

sql_ticket_source,
llm_settings,
):

search_type = question_router(question, sql_search_db_and_model_path)
Expand All @@ -499,10 +496,10 @@ def get_answer_with_settings_with_weaviate_filter(

return get_sql_answer(
question,
model_id,
max_tokens,
model_temperature,
llm_server_url,
llm_settings.model_id,
llm_settings.max_tokens,
llm_settings.model_temperature,
llm_settings.llm_server_url,
sql_search_db_and_model_path,
max_context_length,
sql_ticket_source,
Expand Down Expand Up @@ -530,9 +527,9 @@ def get_answer_with_settings_with_weaviate_filter(
question,
retriever,
client,
model_id,
max_tokens,
model_temperature,
llm_settings.model_id,
llm_settings.max_tokens,
llm_settings.model_temperature,
system_prompt,
)

Expand Down
168 changes: 168 additions & 0 deletions dockers/llm.rag.service/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
from typing import Optional

from pydantic import ConfigDict, Field, field_validator
from pydantic_settings import BaseSettings


SYSTEM_PROMPT_DEFAULT = """You are a specialized support ticket assistant. Format your responses following these rules:
1. Answer the provided question only using the provided context.
2. Do not add the provided context to the generated answer.
3. Include relevant technical details when present or provide a summary of the comments in the ticket.
4. Include the submitter, assignee and collaborator for a ticket when this info is available.
5. If the question cannot be answered with the given context, please say so and do not attempt to provide an answer.
6. Do not create new questions related to the given question, instead answer only the provided question.
7. Provide a clear, direct and factual answer.
"""


def validate_float(value):
if type(value) == float:
return value
try:
return float(value.strip("'").strip('"'))
except (TypeError, ValueError):
raise ValueError("Value must be convertible to a float")


def validate_int(value):
if type(value) == int:
return value
try:
return int(value.strip("'").strip("\""))
except (TypeError, ValueError):
raise ValueError("Value must be convertible to an integer")


class WeaviateSettings(BaseSettings):
model_config = ConfigDict(env_file=".env", extra="ignore")

weaviate_uri: Optional[str] = Field(
None,
alias="WEAVIATE_URI_WITH_PORT",
)
weaviate_grpc_uri: Optional[str] = Field(
None,
alias="WEAVIATE_GRPC_URI_WITH_PORT",
)
weaviate_index_name: Optional[str] = Field(
None,
alias="WEAVIATE_INDEX_NAME",
)
embedding_model_name: Optional[str] = Field(
default="sentence-transformers/all-MiniLM-L6-v2",
alias="EMBEDDING_MODEL_NAME",
)

def is_set(self) -> bool:
return all(
[self.weaviate_uri, self.weaviate_grpc_uri, self.weaviate_index_name]
)

def get_weaviate_uri(self):
return self.weaviate_uri.split(":")[0]

def get_weaviate_port(self):
return int(self.weaviate_uri.split(":")[1])

def get_weaviate_grpc_uri(self):
return self.weaviate_grpc_uri.split(":")[0]

def get_weaviate_grpc_port(self):
return int(self.weaviate_grpc_uri.split(":")[1])


class LlmSettings(BaseSettings):
model_config = ConfigDict(env_file=".env", extra="ignore")

llm_server_url: Optional[str] = Field(
None,
alias="MODEL_LLM_SERVER_URL",
)
model_id: Optional[str] = Field(
default="rubra-ai/Phi-3-mini-128k-instruct",
alias="MODEL_ID",
)
max_tokens: Optional[int] = Field(
default=256,
alias="MAX_TOKENS",
)
model_temperature: Optional[float] = Field(
default=0.01,
alias="MODEL_TEMPERATURE",
)

@field_validator("llm_server_url")
@classmethod
def ensure_v1_endpoint(cls, v: str) -> str:
if not v.endswith("/v1"):
v = v + "/v1"
return v

@field_validator("max_tokens", mode="before")
@classmethod
def validate_max_tokens(cls, v):
return validate_int(v)

@field_validator("model_temperature", mode="before")
@classmethod
def validate_model_temperature(cls, v):
return validate_float(v)


class HybridSearchSettings(BaseSettings):
model_config = ConfigDict(env_file=".env", extra="ignore")

alpha: Optional[float] = Field(
default=0.5,
alias="WEAVIATE_HYBRID_ALPHA",
)
relevant_docs: Optional[int] = Field(
default=2,
alias="RELEVANT_DOCS",
)
system_prompt: Optional[str] = Field(
default=SYSTEM_PROMPT_DEFAULT,
alias="SYSTEM_PROMPT",
)

@field_validator("alpha", mode="before")
@classmethod
def validate_alpha(cls, v):
return validate_float(v)

@field_validator("relevant_docs", mode="before")
@classmethod
def validate_relevant_docs(cls, v):
return validate_int(v)


class SqlSearchSettings(BaseSettings):
model_config = ConfigDict(env_file=".env", extra="ignore")

db_and_model_path: Optional[str] = Field(
default="/app/db/",
alias="SQL_SEARCH_DB_AND_MODEL_PATH",
)
max_context_length: Optional[int] = Field(
default=8192,
alias="MODEL_MAX_CONTEXT_LEN",
)
# TODO: Read sources from DB instead of using this
ticket_source: Optional[str] = Field(
default="https://zendesk.com/api/v2/tickets/",
alias="SQL_TICKET_SOURCE",
)

@field_validator("max_context_length", mode="before")
@classmethod
def validate_max_context_length(cls, v):
return validate_int(v)


class LoggingSettings(BaseSettings):
model_config = ConfigDict(env_file=".env", extra="ignore")

log_file_path: Optional[str] = Field(
default="/app/logs/ragllm.log",
alias="RAGLLM_LOGS_PATH",
)
9 changes: 5 additions & 4 deletions dockers/llm.rag.service/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
import os
from logging.handlers import TimedRotatingFileHandler

# When running locally: export RAGLLM_LOGS_PATH=logs/ragllm.log
log_file_path = os.getenv("RAGLLM_LOGS_PATH") or "/app/logs/ragllm.log"
from config import LoggingSettings

settings = LoggingSettings()
os.makedirs(
os.path.dirname(log_file_path), exist_ok=True
os.path.dirname(settings.log_file_path), exist_ok=True
) # Ensure log directory exists

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d -- %(message)s",
handlers=[
# Log to file, rotate every 1H and store files from last 24 hrs * 7 days files == 168H data
TimedRotatingFileHandler(log_file_path, when="h", interval=1, backupCount=168),
TimedRotatingFileHandler(settings.log_file_path, when="h", interval=1, backupCount=168),
logging.StreamHandler(), # Also log to console
],
)
Expand Down
8 changes: 4 additions & 4 deletions dockers/llm.rag.service/proxy_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import uvicorn

from config import WeaviateSettings


def main():
# Check environment variables to determine which app to run
weaviate_url = os.getenv("WEAVIATE_URI_WITH_PORT")
weaviate_grpc_url = os.getenv("WEAVIATE_GRPC_URI_WITH_PORT")
weaviate_index = os.getenv("WEAVIATE_INDEX_NAME")
weaviate_settings = WeaviateSettings()

host = os.environ.get("APP_HOST", "0.0.0.0")
port = int(os.environ.get("APP_PORT", "8000"))

if weaviate_url and weaviate_grpc_url and weaviate_index:
if weaviate_settings.is_set():
# Run the CSV to Weaviate app
print(f"Starting Weaviate app on {host}:{port}")
uvicorn.run("serverragllm_csv_to_weaviate_local:app", host=host, port=port)
Expand Down
Loading