Skip to content

Commit

Permalink
Merge pull request #294 from l3vels/feat/run-logs
Browse files Browse the repository at this point in the history
feat: start and end dates in run logs, add is chat history bool, add toolkit id
  • Loading branch information
Chkhikvadze authored Nov 9, 2023
2 parents 423a0b0 + 62bf0cb commit 7965905
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 59 deletions.
10 changes: 9 additions & 1 deletion apps/server/models/run_log.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import uuid
from datetime import datetime
from typing import Dict

from sqlalchemy import UUID, Boolean, Column, ForeignKey, String
from sqlalchemy import (UUID, Boolean, Column, DateTime, ForeignKey, Integer,
String)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Session, relationship
from sqlalchemy.sql import or_
Expand Down Expand Up @@ -32,6 +34,11 @@ class RunLogModel(BaseModel):
type = Column(String) # LLM, Tool
messages = Column(JSONB)

start_date = Column(DateTime(timezone=True), default=datetime.utcnow)
end_date = Column(DateTime(timezone=True))

toolkit_id = Column(UUID, nullable=True)

run_id = Column(
UUID, ForeignKey("run.id", ondelete="CASCADE"), nullable=True, index=True
)
Expand Down Expand Up @@ -131,6 +138,7 @@ def add_message_to_latest_run_log(
new_messages = list(old_run_log.messages) if old_run_log.messages else []
new_messages.append(message)

old_run_log.end_date = datetime.utcnow()
old_run_log.messages = new_messages
old_run_log.modified_by = user_id

Expand Down
19 changes: 17 additions & 2 deletions apps/server/services/run_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy.orm import Session

from models.run_log import RunLogModel
from tools.get_tools import get_toolkit_id_by_tool_name
from typings.run import RunLogInput, RunLogType


Expand Down Expand Up @@ -42,7 +43,11 @@ def get_tool_callback_handler(self) -> BaseCallbackHandler:
return callback_handler

def create_run_log(
self, type: RunLogType, name: Optional[str] = "", messages: Optional[Dict] = []
self,
type: RunLogType,
name: Optional[str] = "",
messages: Optional[Dict] = [],
toolkit_id: Optional[UUID] = None,
):
return RunLogModel.create_run_log(
self.session,
Expand All @@ -54,6 +59,7 @@ def create_run_log(
name=name,
type=str(type),
messages=messages,
toolkit_id=toolkit_id,
),
self.user_id,
self.account_id,
Expand All @@ -70,6 +76,8 @@ def create_llm_run_log(self, messages: List[BaseMessage]):
{
"name": message_mapping[message.type],
"content": message.content,
"additional_kwargs": message.additional_kwargs,
"is_chat_history": message.additional_kwargs.get("uuid") is not None,
}
for message in messages
]
Expand All @@ -86,7 +94,14 @@ def create_tool_run_log(self, name: str, input: str):
}
]

return self.create_run_log(type=RunLogType.TOOL, name=name, messages=messages)
toolkit_id = get_toolkit_id_by_tool_name(name)

return self.create_run_log(
type=RunLogType.TOOL,
name=name,
messages=messages,
toolkit_id=UUID(toolkit_id),
)

def add_message_to_run_log(self, type: RunLogType, name: str, content: str):
return RunLogModel.add_message_to_latest_run_log(
Expand Down
9 changes: 9 additions & 0 deletions apps/server/tools/get_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def get_all_tools():
return result


def get_toolkit_id_by_tool_name(tool_name: str) -> str | None:
toolkits = get_all_tools()

for toolkit in toolkits:
for tool in toolkit["tools"]:
if tool["name"] == tool_name:
return toolkit["toolkit_id"]


def get_agent_tools(
toolkit_ids: List[str], db, account, settings, agent_with_configs, callback_handler
) -> List[BaseTool]:
Expand Down
12 changes: 11 additions & 1 deletion apps/server/typings/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ class RunLogInput(BaseModel):
session_id: Optional[str]
name: Optional[str]
messages: Optional[List[Dict]]
toolkit_id: Optional[UUID4]


class RunLogMessageOutput(BaseModel):
name: str
content: str
is_chat_history: Optional[bool]


class RunLogOutput(BaseModel):
id: UUID4
name: str
type: str
messages: Optional[List[Dict]]
messages: Optional[List[RunLogMessageOutput]]
start_date: Optional[str]
end_date: Optional[str]
toolkit_id: Optional[UUID4]
4 changes: 4 additions & 0 deletions apps/ui/src/gql/ai/run/runLogs.gql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ query runLogs($run_id: run_id!) @api(name: "ai") {
messages {
name
content
is_chat_history
}
start_date
end_date
created_on
toolkit_id
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import styled from 'styled-components'
import { RunLog } from 'types'

type RunLogMessagesProps = {
log: RunLog
}

const RunLogMessages = ({ log }: RunLogMessagesProps) => {
const { messages } = log

return (
<StyledCards>
{messages.map(({ name, content, is_chat_history }, index: number) => {
// TODO: use is_chat_history to render chat history in collapse
return (
<StyledCard key={index}>
<StyledTitle>{name}</StyledTitle>

{content && (
<StyledCodeCard>
<StyledCodeContent>{content}</StyledCodeContent>
</StyledCodeCard>
)}
</StyledCard>
)
})}
</StyledCards>
)
}

export default RunLogMessages

const StyledCards = styled.div`
display: flex;
flex-direction: column;
align-items: stretch;
width: 100%;
gap: 20px;
`

const StyledCard = styled.div`
border-radius: 10px;
padding: 20px;
width: 100%;
background: #fff;
color: #000;
`

const StyledTitle = styled.h2`
margin: 0;
padding: 0;
font-size: 18px;
color: #000;
`

const StyledCodeCard = styled.div`
margin-top: 15px;
padding: 16px;
background-color: #f1f1f1;
border-radius: 10px;
`

const StyledCodeContent = styled.pre`
margin: 0;
padding: 0;
font-family: monospace;
white-space: pre-wrap;
font-size: 12px;
color: #000;
`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './RunLogMessages'
58 changes: 3 additions & 55 deletions apps/ui/src/modals/RunLogsModal/RunLogs/RunLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TabPanels from '@l3-lib/ui-core/dist/TabPanels'
import TabsContext from '@l3-lib/ui-core/dist/TabsContext'
import { useState } from 'react'
import { StyledFormTabList, StyledFormTabsWrapper } from 'pages/Agents/AgentForm/AgentForm'
import RunLogMessages from './RunLogMessages'

type RunLogsProps = {
runId: string
Expand Down Expand Up @@ -43,23 +44,9 @@ const RunLogs = ({ runId }: RunLogsProps) => {

<TabsContext activeTabId={activeTab}>
<TabPanels noAnimation>
{data.map(({ messages }, index) => (
{data.map((log, index) => (
<TabPanel key={index}>
<StyledCards>
{messages.map(({ name, content }, index: number) => {
return (
<StyledLogCard key={index}>
<StyledLogTitle>{name}</StyledLogTitle>

{content && (
<StyledCodeCard>
<StyledCodeContent>{content}</StyledCodeContent>
</StyledCodeCard>
)}
</StyledLogCard>
)
})}
</StyledCards>
<RunLogMessages log={log} />
</TabPanel>
))}
</TabPanels>
Expand All @@ -78,14 +65,6 @@ const StyledWrapper = styled.div`
overflow-y: scroll;
`

const StyledCards = styled.div`
display: flex;
flex-direction: column;
align-items: stretch;
width: 100%;
gap: 20px;
`

const StyledLoaderWrapper = styled.div`
position: absolute;
width: 40px;
Expand All @@ -96,34 +75,3 @@ const StyledLoaderWrapper = styled.div`
margin-bottom: 20px;
margin-left: 5px;
`

const StyledLogCard = styled.div`
border-radius: 10px;
padding: 20px;
width: 100%;
background: #fff;
color: #000;
`

const StyledLogTitle = styled.h2`
margin: 0;
padding: 0;
font-size: 18px;
color: #000;
`

const StyledCodeCard = styled.div`
margin-top: 15px;
padding: 16px;
background-color: #f1f1f1;
border-radius: 10px;
`

const StyledCodeContent = styled.pre`
margin: 0;
padding: 0;
font-family: monospace;
white-space: pre-wrap;
font-size: 12px;
color: #000;
`
6 changes: 6 additions & 0 deletions apps/ui/src/types/run.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Nullable } from './utils'

export interface RunLog {
id: string
name: string
type: 'LLM' | 'Tool'
messages: {
name: string
content: string
is_chat_history: Nullable<boolean>
}[]
created_on: string
start_date: Nullable<string>
end_date: Nullable<string>
toolkit_id: Nullable<string>
}

0 comments on commit 7965905

Please sign in to comment.