Skip to content
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

feat(product-assistant): enhanced trends generation #25484

Merged
merged 178 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
178 commits
Select commit Hold shift + click to select a range
ef47d15
chore: deps
skoob13 Oct 9, 2024
12cb441
feat: initial graph
skoob13 Oct 9, 2024
a140d0d
feat: partial toolkit
skoob13 Oct 9, 2024
fe077b7
Update query snapshots
github-actions[bot] Oct 9, 2024
c40c9b1
Update query snapshots
github-actions[bot] Oct 9, 2024
50131c7
feat: tools
skoob13 Oct 9, 2024
46d96be
Update query snapshots
github-actions[bot] Oct 9, 2024
888725e
Update query snapshots
github-actions[bot] Oct 9, 2024
5b18198
feat: tool validation
skoob13 Oct 10, 2024
357b65a
feat: team query runner
skoob13 Oct 10, 2024
2d6f8b1
feat: tool loop
skoob13 Oct 10, 2024
6bd460a
feat: trend generation step
skoob13 Oct 10, 2024
90b6e30
refactor: stateful nodes
skoob13 Oct 10, 2024
6dd42e1
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 10, 2024
ab8761e
Update query snapshots
github-actions[bot] Oct 10, 2024
a701c38
Update query snapshots
github-actions[bot] Oct 10, 2024
d86cf52
Update query snapshots
github-actions[bot] Oct 10, 2024
8bfd076
feat: handle unformatted response
skoob13 Oct 10, 2024
18a535c
feat: pydantic schemas for state
skoob13 Oct 10, 2024
c879e8d
fix: state
skoob13 Oct 10, 2024
e3b8c69
feat: initial properties
skoob13 Oct 10, 2024
432e06c
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 10, 2024
97131b8
Update query snapshots
github-actions[bot] Oct 10, 2024
4a87b38
Update query snapshots
github-actions[bot] Oct 10, 2024
0db66fe
feat: support multiple arguments in tools
skoob13 Oct 11, 2024
c621378
feat: retrieve event properties
skoob13 Oct 11, 2024
b534431
chore: docs
skoob13 Oct 11, 2024
2110985
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 11, 2024
999f365
feat: entity validation in entity prop values retrieval
skoob13 Oct 11, 2024
2f4a90b
Update query snapshots
github-actions[bot] Oct 11, 2024
dc6194e
Update query snapshots
github-actions[bot] Oct 11, 2024
5b81a53
Update query snapshots
github-actions[bot] Oct 11, 2024
0ad38bf
fix: steps joining
skoob13 Oct 11, 2024
ba48f5a
feat: session retrieval
skoob13 Oct 11, 2024
5784c01
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 11, 2024
02483bb
Update query snapshots
github-actions[bot] Oct 11, 2024
69e002b
Update query snapshots
github-actions[bot] Oct 11, 2024
b466036
feat: wip conversations
skoob13 Oct 14, 2024
184a2e1
feat: agent conversation
skoob13 Oct 14, 2024
935a277
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 15, 2024
14b01e5
feat: reconstruct message history
skoob13 Oct 15, 2024
712e221
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 15, 2024
ce7d100
chore: __init__.py
skoob13 Oct 16, 2024
e662927
chore: execution limits
skoob13 Oct 17, 2024
7b8e03e
feat: migrating to a new assistant
skoob13 Oct 17, 2024
9227519
feat: streaming (wip)
skoob13 Oct 17, 2024
8ebf5df
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
707f6ec
refactor: query endpoint
skoob13 Oct 18, 2024
939247f
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 18, 2024
f589afa
feat: streaming
skoob13 Oct 18, 2024
e9b64b2
fix: streaming of plans
skoob13 Oct 18, 2024
507766b
merge
skoob13 Oct 18, 2024
e63d194
Update query snapshots
github-actions[bot] Oct 18, 2024
e47471a
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
3d371a8
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
e5fda3b
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
0605516
Update query snapshots
github-actions[bot] Oct 18, 2024
c39c19c
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
0a63d97
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 18, 2024
16411eb
Update query snapshots
github-actions[bot] Oct 18, 2024
dc04a7d
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
5abc6f1
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 18, 2024
518ed05
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
90b0a56
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
e62ae94
Update query snapshots
github-actions[bot] Oct 18, 2024
e187e5c
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
0087853
Update query snapshots
github-actions[bot] Oct 18, 2024
ecb78aa
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
6aac927
Update query snapshots
github-actions[bot] Oct 18, 2024
7ad390a
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
38e05a8
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
a431bcc
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
3aaffb5
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
7bbd2c9
Update query snapshots
github-actions[bot] Oct 18, 2024
e17cfc8
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
6106b0b
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 21, 2024
3f3ded3
chore: sync mypy
skoob13 Oct 21, 2024
1e15a3a
chore: sync mypy
skoob13 Oct 21, 2024
a2e5fba
Update query snapshots
github-actions[bot] Oct 21, 2024
3e57e2c
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
85462a2
Revert "chore: sync mypy"
skoob13 Oct 21, 2024
6c4d411
chore: mypy
skoob13 Oct 21, 2024
21bdeca
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 21, 2024
a7c65a5
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
ff530b5
chore: another mypy attempt
skoob13 Oct 21, 2024
afdb6b5
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 21, 2024
5dafdd7
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
6d03672
Update query snapshots
github-actions[bot] Oct 21, 2024
c1cf1dc
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
7929d62
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
1e405f3
Update query snapshots
github-actions[bot] Oct 21, 2024
65845fc
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
3e677f4
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 21, 2024
90cfd44
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
0f64ac3
fix: toolkit bugs for entity props and values
skoob13 Oct 21, 2024
acdefed
test: event properties and values
skoob13 Oct 21, 2024
f1ea562
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 21, 2024
f55038f
Update query snapshots
github-actions[bot] Oct 21, 2024
c05d779
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
ff9ba61
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 21, 2024
16d68e5
Update query snapshots
github-actions[bot] Oct 21, 2024
e32bb98
Update query snapshots
github-actions[bot] Oct 21, 2024
6644ea1
chore: prompt tweaks
skoob13 Oct 21, 2024
1ff8c64
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 21, 2024
85c53d1
fix: filter thoughts without an output
skoob13 Oct 22, 2024
ffe079f
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
e8b8079
chore: langfuse for debugging
skoob13 Oct 22, 2024
9ab0462
chore: slightly tweak prompt for math
skoob13 Oct 22, 2024
3e81442
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
0dc542c
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
205a112
feat: tweak formulas and aggregation
skoob13 Oct 22, 2024
e74e1ee
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 22, 2024
8a8dd68
Update query snapshots
github-actions[bot] Oct 22, 2024
ec2deb9
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
74c13e6
fix: deps
skoob13 Oct 22, 2024
95ff176
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 22, 2024
3869f24
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
af276a2
fix: schema
skoob13 Oct 22, 2024
ba3fced
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 22, 2024
c2baa6e
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
75c0987
Update query snapshots
github-actions[bot] Oct 22, 2024
8f7eaa5
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
d9bbc43
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
559f6fc
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
a3c1402
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 22, 2024
83d3e86
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 22, 2024
325cae2
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 22, 2024
98a13b3
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 22, 2024
af843c4
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 22, 2024
6d28818
Update query snapshots
github-actions[bot] Oct 22, 2024
6f48fa1
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 23, 2024
4f794e0
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 23, 2024
048883b
Update query snapshots
github-actions[bot] Oct 23, 2024
5669bce
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 23, 2024
aa50832
Update query snapshots
github-actions[bot] Oct 23, 2024
0229cf4
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 23, 2024
8d013b8
Update query snapshots
github-actions[bot] Oct 23, 2024
c4c2bae
Reorder node metods for readability
Twixes Oct 23, 2024
8d4f52e
fix: mypy glitch
skoob13 Oct 24, 2024
5e84f3d
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 24, 2024
8c73c09
revert: mypy-baseline
skoob13 Oct 24, 2024
7a1f0a1
fix: mypy errors after dependencies upgrade
skoob13 Oct 24, 2024
deb95c5
fix: remove bunch of todos
skoob13 Oct 24, 2024
edba428
fix: remove cohorts completely
skoob13 Oct 24, 2024
a8b4e84
test: test flow
skoob13 Oct 24, 2024
deaa5e8
chore: mypy baseline
skoob13 Oct 24, 2024
3dadcdc
fix: do not show langfuse warning
skoob13 Oct 24, 2024
800ff09
fix: unify hardcoded event mapping
skoob13 Oct 24, 2024
7dabb27
Update query snapshots
github-actions[bot] Oct 24, 2024
8fcfae3
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 24, 2024
23800f1
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 24, 2024
ba9f7a6
fix: distinct messages
skoob13 Oct 24, 2024
ad7861f
fix: send messages
skoob13 Oct 25, 2024
906f809
feat: straighforward message inferring on frontend
skoob13 Oct 25, 2024
78476b7
fix: only stream generation step chunks
skoob13 Oct 25, 2024
876c405
fix: thread story
skoob13 Oct 25, 2024
5be2b0d
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 25, 2024
c997de4
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
84a97c4
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
6eab798
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
38fdf15
Update query snapshots
github-actions[bot] Oct 25, 2024
4fbe00a
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
7b242c5
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
d7b4539
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
39e0f22
Update query snapshots
github-actions[bot] Oct 25, 2024
de38a76
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
c8e74ab
Update query snapshots
github-actions[bot] Oct 25, 2024
b427cff
fix: disable flaky test
skoob13 Oct 25, 2024
96b0213
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 25, 2024
45b3a4c
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 25, 2024
2252b1c
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 25, 2024
d4f48c2
Merge branch 'master' of github.com:PostHog/posthog into feat/trends-…
skoob13 Oct 25, 2024
c7263c9
Merge branch 'feat/trends-agent' of github.com:PostHog/posthog into f…
skoob13 Oct 25, 2024
6ba7fa2
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 25, 2024
4aac54c
Update query snapshots
github-actions[bot] Oct 25, 2024
9d68b26
Update query snapshots
github-actions[bot] Oct 25, 2024
2a59b8b
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 25, 2024
476decb
Update query snapshots
github-actions[bot] Oct 25, 2024
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
101 changes: 101 additions & 0 deletions ee/hogai/assistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from collections.abc import Generator
from typing import Any, Literal, TypedDict, TypeGuard, Union, cast

from langchain_core.messages import AIMessageChunk
from langfuse.callback import CallbackHandler
from langgraph.graph.state import StateGraph

from ee import settings
from ee.hogai.trends.nodes import CreateTrendsPlanNode, CreateTrendsPlanToolsNode, GenerateTrendsNode
from ee.hogai.utils import AssistantNodeName, AssistantState, Conversation
from posthog.models.team.team import Team
from posthog.schema import VisualizationMessage

if settings.LANGFUSE_PUBLIC_KEY:
langfuse_handler = CallbackHandler(
public_key=settings.LANGFUSE_PUBLIC_KEY, secret_key=settings.LANGFUSE_SECRET_KEY, host=settings.LANGFUSE_HOST
)
else:
langfuse_handler = None


def is_value_update(update: list[Any]) -> TypeGuard[tuple[Literal["values"], dict[AssistantNodeName, Any]]]:
"""
Transition between nodes.
"""
return len(update) == 2 and update[0] == "updates"


class LangGraphState(TypedDict):
langgraph_node: AssistantNodeName


def is_message_update(
update: list[Any],
) -> TypeGuard[tuple[Literal["messages"], tuple[Union[AIMessageChunk, Any], LangGraphState]]]:
"""
Streaming of messages. Returns a partial state.
"""
return len(update) == 2 and update[0] == "messages"


class Assistant:
_team: Team
_graph: StateGraph

def __init__(self, team: Team):
self._team = team
self._graph = StateGraph(AssistantState)

def _compile_graph(self):
builder = self._graph

create_trends_plan_node = CreateTrendsPlanNode(self._team)
builder.add_node(CreateTrendsPlanNode.name, create_trends_plan_node.run)

create_trends_plan_tools_node = CreateTrendsPlanToolsNode(self._team)
builder.add_node(CreateTrendsPlanToolsNode.name, create_trends_plan_tools_node.run)

generate_trends_node = GenerateTrendsNode(self._team)
builder.add_node(GenerateTrendsNode.name, generate_trends_node.run)

builder.add_edge(AssistantNodeName.START, create_trends_plan_node.name)
builder.add_conditional_edges(create_trends_plan_node.name, create_trends_plan_node.router)
builder.add_conditional_edges(create_trends_plan_tools_node.name, create_trends_plan_tools_node.router)
builder.add_conditional_edges(GenerateTrendsNode.name, generate_trends_node.router)

return builder.compile()

def stream(self, conversation: Conversation) -> Generator[str, None, None]:
assistant_graph = self._compile_graph()
callbacks = [langfuse_handler] if langfuse_handler else []
messages = [message.root for message in conversation.messages]

generator = assistant_graph.stream(
{"messages": messages},
config={"recursion_limit": 24, "callbacks": callbacks},
stream_mode=["messages", "updates"],
)

chunks = AIMessageChunk(content="")

for update in generator:
if is_value_update(update):
_, state_update = update
if (
AssistantNodeName.GENERATE_TRENDS in state_update
and "messages" in state_update[AssistantNodeName.GENERATE_TRENDS]
):
message = cast(VisualizationMessage, state_update[AssistantNodeName.GENERATE_TRENDS]["messages"][0])
yield message.model_dump_json()
elif is_message_update(update):
langchain_message, langgraph_state = update[1]
if langgraph_state["langgraph_node"] == AssistantNodeName.GENERATE_TRENDS and isinstance(
langchain_message, AIMessageChunk
):
chunks += langchain_message # type: ignore
parsed_message = GenerateTrendsNode.parse_output(chunks.tool_calls[0]["args"])
if parsed_message:
yield VisualizationMessage(
reasoning_steps=parsed_message.reasoning_steps, answer=parsed_message.answer
).model_dump_json()
55 changes: 0 additions & 55 deletions ee/hogai/generate_trends_agent.py

This file was deleted.

6 changes: 3 additions & 3 deletions ee/hogai/hardcoded_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"$identify": {
"label": "Identify",
"description": "A user has been identified with properties",
"description": "Identifies an anonymous user. This event doesn't show how many users you have but rather how many users used an account.",
},
"$create_alias": {
"label": "Alias",
Expand Down Expand Up @@ -915,8 +915,8 @@
"session_properties": {
"$session_duration": {
"label": "Session duration",
"description": "The duration of the session being tracked. Learn more about how PostHog tracks sessions in our documentation.\n\nNote, if the duration is formatted as a single number (not 'HH:MM:SS'), it's in seconds.",
"examples": ["01:04:12"],
"description": "The duration of the session being tracked in seconds.",
"examples": ["30", "146", "2"],
"type": "Numeric",
},
"$start_timestamp": {
Expand Down
77 changes: 0 additions & 77 deletions ee/hogai/system_prompt.py

This file was deleted.

Loading
Loading