Skip to content

Commit

Permalink
chore: update packages and add literal ai tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
epigos committed Jan 19, 2025
1 parent 9c2245c commit d6cc724
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 191 deletions.
25 changes: 11 additions & 14 deletions app/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime

from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.messages import BaseMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.base import BaseCheckpointSaver
Expand Down Expand Up @@ -35,22 +35,19 @@ async def stream(
"messages": [HumanMessage(content=message)],
"today": datetime.now().isoformat(),
}
async for event in self._graph.astream_events(
async for msg, metadata in self._graph.astream(
inputs,
config=config,
version="v2",
stream_mode="messages",
):
kind = event["event"]
metadata = event["metadata"]
if kind != "on_chat_model_stream" or metadata["langgraph_node"] != "agent":
continue
chunk = event["data"]["chunk"]
if not isinstance(chunk, AIMessage):
continue
if not chunk.content:
continue

yield utils.get_message_text(chunk)
msg = typing.cast(BaseMessage, msg)
metadata = typing.cast(dict[str, typing.Any], metadata)
if (
msg.content
and not isinstance(msg, HumanMessage)
and metadata["langgraph_node"] == "agent"
):
yield utils.get_message_text(msg)

async def invoke(self, message: str, config: RunnableConfig) -> str:
inputs = {
Expand Down
2 changes: 2 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Settings(BaseSettings):
retriever_threshold: float = 0.3
# openai config
openai_api_key: pydantic.SecretStr = pydantic.SecretStr("")
# chainlit
literal_api_key: str | None = None

model_config = SettingsConfigDict(
env_file=f"{root_dir}/.env", env_file_encoding="utf-8"
Expand Down
5 changes: 5 additions & 0 deletions app/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def get_logging_config() -> typing.Dict[str, typing.Any]:
"level": "INFO",
"propagate": False,
},
"langchain_core": {
"handlers": ["default"],
"level": "ERROR",
"propagate": False,
},
},
}

Expand Down
7 changes: 6 additions & 1 deletion app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def load_chat_model(
"""
provider, model = fully_specified_name.split(":", maxsplit=1)
return init_chat_model(
model, model_provider=provider, temperature=temperature, max_tokens=max_tokens
model,
model_provider=provider,
temperature=temperature,
max_tokens=max_tokens,
stream_usage=True,
max_retries=2,
)


Expand Down
Loading

0 comments on commit d6cc724

Please sign in to comment.