Skip to content

Commit

Permalink
prompt fixes for posthog; toolcall response fix (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
kektobiologist authored Nov 12, 2024
1 parent a542015 commit 112af41
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
11 changes: 6 additions & 5 deletions apps/src/posthog/actionDescriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ export const ACTION_DESCRIPTIONS: ActionDescription[] = [
},
description: 'Gets the schemas of the specified tables by their ids in the database.',
},
{
name: "getHogQLExpressionsDocumentation",
args: {},
description: "Gets the documentation for HogQL expressions. Use this tool if you need help with writing HogQL.",
},
// NOTE(@arpit): adding this to default system prompt for now.
// {
// name: "getHogQLExpressionsDocumentation",
// args: {},
// description: "Gets the documentation for HogQL expressions. Use this tool if you need help with writing HogQL.",
// },
{
name: "getEventCommonProperties",
args: {
Expand Down
10 changes: 5 additions & 5 deletions apps/src/posthog/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,23 @@ export interface CommonPropertiesResponse {
property_type: string;
is_seen_on_filtered_events: boolean;
tags: string[];
}
}[]
}

export const getEventCommonProperties = async (eventNames: string[]) => {
const projectId = await memoizedGetCurrentProjectId()
if (projectId) {
// a very shitty url but whatever
const commonPropertiesUrl = `/api/projects/${projectId}/property_definitions?limit=5&event_names=${JSON.stringify(eventNames.join(','))}&excluded_properties=${JSON.stringify(excluded_properties.join(','))}&is_feature_flag=false `
const response = await RPCs.fetchData(
const commonPropertiesUrl = `/api/projects/${projectId}/property_definitions?limit=20&event_names=${JSON.stringify(eventNames.join(','))}&excluded_properties=${JSON.stringify(excluded_properties.join(','))}&is_feature_flag=false `
const response: CommonPropertiesResponse = await RPCs.fetchData(
commonPropertiesUrl,
'GET',
undefined,
{cookieKey: 'posthog_csrftoken', headerKey: 'X-Csrftoken'}
) as CommonPropertiesResponse
return response
} else {
console.warn("No current project found")
return []
console.warn("[minusx] No current project found in getEventCommonProperties")
return
}
}
30 changes: 22 additions & 8 deletions apps/src/posthog/appController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,33 @@ export class PosthogController extends AppController<PosthogAppState> {
}
return actionContent;
}
async getHogQLExpressionsDocumentation() {
const actionContent: BlankMessageContent = {
type: "BLANK",
};
actionContent.content = expressionsMd;
return actionContent;
}
// NOTE(@arpit): adding this to default system prompt for now.
// async getHogQLExpressionsDocumentation() {
// const actionContent: BlankMessageContent = {
// type: "BLANK",
// };
// actionContent.content = expressionsMd;
// return actionContent;
// }

// NOTE(@arpit): sometimes minusx calls this tool with a property name instead of event names;
// we should probably have another tool to look for common properties?
// eg. user asks for split on device; we don't know which event specifically has the device property
// minusx should be able to look up for a property similar to 'device' and then just
// write a query like 'select count(*), properties.$device_type from events'
async getEventCommonProperties({event_names}: {event_names: string[]}) {
const actionContent: BlankMessageContent = {
type: "BLANK",
};
const commonProperties = await getEventCommonProperties(event_names);
actionContent.content = JSON.stringify(commonProperties, null, 2);
if (commonProperties) {
// omit id, is_seen_on_filtered_events, tags
let results = commonProperties.results.map((result) => {
const {id, is_seen_on_filtered_events, tags, ...rest} = result
return rest
})
actionContent.content = JSON.stringify(results, null, 2);
}
return actionContent;
}
async runBackgroundHogqlQuery({query}: {query: string}) {
Expand Down
2 changes: 2 additions & 0 deletions apps/src/posthog/docs/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ HogQL expressions can access data like:

Properties can be accessed with dot notation like `person.properties.$initial_browser` which also works for nested or JSON properties. They can also be accessed with bracket notation like `properties['$feature/cool-flag']`.

> **Note:** `properties["$feature/cool-flag"]` is invalid syntax. You need to use single quotes like `properties['$feature/cool-flag']`.
> **Note:** PostHog's properties always include `$` as a prefix, while custom properties do not (unless you add it).
Property identifiers must be known at query time. For dynamic access, use the JSON manipulation functions from below on the `properties` field directly.
Expand Down
8 changes: 7 additions & 1 deletion apps/src/posthog/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getDescriptionsForEventDefinitions } from "./operations";
import { PosthogAppStateSchema } from "./stateSchema";
import hardcodedEventDefinitions from "./docs/hardcoded-event-definitions.json";
import { formatEventDescriptionsAsYaml } from "./operations";
import expressionsMd from "./docs/expressions.md?raw";

export const DEFAULT_SYSTEM_PROMPT = `You are a master of Posthog and HogQL (which is a flavor of SQL for ClickHouse databases).
Expand All @@ -21,7 +22,6 @@ Routine to follow:
4. Determine if you need to add HogQL, if so call the updateHogQLQueryAndExecute tool.
5. If you estimate that the task can be accomplished with the tool calls selected in the current call, include the markTaskDone tool call at the end. Do not wait for everything to be executed.
6. If you are waiting for the user's clarification, also mark the task as done.
7. If you feel you need help with writing HogQL, call the getHogQLExpressionsDocumentation tool.
For your reference, there is a description of the data model.
Expand All @@ -44,6 +44,12 @@ Here is a detailed list of event definitions and their descriptions.
${formatEventDescriptionsAsYaml(getDescriptionsForEventDefinitions(hardcodedEventDefinitions))}
</EventDefinitions>
Here is documentation for HogQL expressions:
<HogQLExpressionsDocs>
${expressionsMd}
</HogQLExpressionsDocs>
<AppStateSchema>
${JSON.stringify(PosthogAppStateSchema)}
</AppStateSchema>
Expand Down

0 comments on commit 112af41

Please sign in to comment.