Skip to content

Commit

Permalink
chore: upgrade OpenAI client (#19986)
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin authored Jan 29, 2024
1 parent 6437d36 commit 974553f
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 55 deletions.
4 changes: 1 addition & 3 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1538,9 +1538,7 @@ const api = {
return await new ApiRequest().recording(recordingId).withAction('persist').create()
},

async summarize(
recordingId: SessionRecordingType['id']
): Promise<{ content: string; ai_result: Record<string, any> }> {
async summarize(recordingId: SessionRecordingType['id']): Promise<{ content: string }> {
return await new ApiRequest().recording(recordingId).withAction('summarize').create()
},

Expand Down
2 changes: 1 addition & 1 deletion posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def get_decide(request: HttpRequest):
}

if isinstance(team.session_replay_config, Dict):
record_canvas = team.session_replay_config["record_canvas"] or False
record_canvas = team.session_replay_config.get("record_canvas", False)
session_recording_response.update(
{
"recordCanvas": record_canvas,
Expand Down
15 changes: 8 additions & 7 deletions posthog/hogql/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def write_sql_from_prompt(prompt: str, *, current_query: Optional[str] = None, t
)
)
instance_region = get_instance_region() or "HOBBY"
messages = [
messages: list[openai.types.chat.ChatCompletionMessageParam] = [
{"role": "system", "content": IDENTITY_MESSAGE},
{
"role": "system",
Expand Down Expand Up @@ -101,17 +101,18 @@ def write_sql_from_prompt(prompt: str, *, current_query: Optional[str] = None, t
prompt_tokens_total, completion_tokens_total = 0, 0
for _ in range(3): # Try up to 3 times in case the generated SQL is not valid HogQL
attempt_count += 1
result = openai.ChatCompletion.create(
result = openai.chat.completions.create(
model="gpt-3.5-turbo",
temperature=0.8,
messages=messages,
user=f"{instance_region}/{user.pk}", # The user ID is for tracking within OpenAI in case of overuse/abuse
)
content: str = result["choices"][0]["message"]["content"].removesuffix(";")
prompt_tokens_last = result["usage"]["prompt_tokens"]
completion_tokens_last = result["usage"]["completion_tokens"]
prompt_tokens_total += prompt_tokens_last
completion_tokens_total += completion_tokens_last
content: str = ""
if result.choices[0] and result.choices[0].message.content:
content = result.choices[0].message.content.removesuffix(";")
if result.usage:
prompt_tokens_total += result.usage.prompt_tokens
completion_tokens_total += result.usage.completion_tokens
if content.startswith(UNCLEAR_PREFIX):
error = content.removeprefix(UNCLEAR_PREFIX).strip()
break
Expand Down
66 changes: 33 additions & 33 deletions posthog/session_recordings/session_summary/summarize_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Dict, Any

import openai

from prometheus_client import Histogram

from posthog.api.activity_log import ServerTimingsGathered
Expand Down Expand Up @@ -288,57 +289,56 @@ def summarize_recording(recording: SessionRecording, user: User, team: Team):
)

instance_region = get_instance_region() or "HOBBY"
messages = [
{
"role": "system",
"content": """

with timer("openai_completion"):
result = openai.chat.completions.create(
# model="gpt-4-1106-preview", # allows 128k tokens
model="gpt-4", # allows 8k tokens
temperature=0.7,
messages=[
{
"role": "system",
"content": """
Session Replay is PostHog's tool to record visits to web sites and apps.
We also gather events that occur like mouse clicks and key presses.
You write two or three sentence concise and simple summaries of those sessions based on a prompt.
You are more likely to mention errors or things that look like business success such as checkout events.
You don't help with other knowledge.""",
},
{
"role": "user",
"content": f"""the session metadata I have is {session_metadata_dict}.
},
{
"role": "user",
"content": f"""the session metadata I have is {session_metadata_dict}.
it gives an overview of activity and duration""",
},
{
"role": "user",
"content": f"""
},
{
"role": "user",
"content": f"""
URLs associated with the events can be found in this mapping {prompt_data.url_mapping}.
""",
},
{
"role": "user",
"content": f"""the session events I have are {prompt_data.results}.
},
{
"role": "user",
"content": f"""the session events I have are {prompt_data.results}.
with columns {prompt_data.columns}.
they give an idea of what happened and when,
if present the elements_chain extracted from the html can aid in understanding
but should not be directly used in your response""",
},
{
"role": "user",
"content": """
},
{
"role": "user",
"content": """
generate a two or three sentence summary of the session.
use as concise and simple language as is possible.
assume a reading age of around 12 years old.
generate no text other than the summary.""",
},
]

with timer("openai_completion"):
result = openai.ChatCompletion.create(
# model="gpt-4-1106-preview", # allows 128k tokens
model="gpt-4", # allows 8k tokens
temperature=0.7,
messages=messages,
user=f"{instance_region}/{user.pk}", # The user ID is for tracking within OpenAI in case of overuse/abuse
},
],
user=f"{instance_region}/{user.pk}", # allows 8k tokens
)

usage = result.get("usage", {}).get("prompt_tokens", None)
usage = result.usage.prompt_tokens if result.usage else None
if usage:
TOKENS_IN_PROMPT_HISTOGRAM.observe(usage)

content: str = result.get("choices", [{}])[0].get("message", {}).get("content", "")
return {"ai_result": result, "content": content, "prompt": messages, "timings": timer.get_all_timings()}
content: str = result.choices[0].message.content or ""
return {"content": content, "timings": timer.get_all_timings()}
2 changes: 1 addition & 1 deletion requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ types-redis==4.3.20
types-retry==0.9.9.4
types-requests==2.26.1
parameterized==0.9.0
pytest==7.4.0
pytest==7.4.4
pytest-asyncio==0.21.1
pytest-cov==4.1.0
pytest-django==4.5.2
Expand Down
8 changes: 5 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ docopt==0.6.2
# via pytest-watch
email-validator==2.0.0.post2
# via pydantic
exceptiongroup==1.1.2
# via pytest
exceptiongroup==1.2.0
# via
# -c requirements.txt
# pytest
faker==17.5.0
# via -r requirements-dev.in
fakeredis[lua]==2.11.0
Expand Down Expand Up @@ -149,7 +151,7 @@ pydantic-core==2.14.6
# pydantic
pyproject-hooks==1.0.0
# via build
pytest==7.4.0
pytest==7.4.4
# via
# -r requirements-dev.in
# pytest-asyncio
Expand Down
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mimesis==5.2.1
more-itertools==9.0.0
django-two-factor-auth==1.14.0
phonenumberslite==8.13.6
openai==0.27.8
openai==1.10.0
nh3==0.2.14
hogql-parser==1.0.3
urllib3[secure,socks]==1.26.18
36 changes: 30 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ aiohttp==3.9.0
# -r requirements.in
# aiobotocore
# geoip2
# openai
# s3fs
aioitertools==0.11.0
# via aiobotocore
Expand All @@ -32,6 +31,10 @@ annotated-types==0.5.0
# via pydantic
antlr4-python3-runtime==4.13.1
# via -r requirements.in
anyio==4.2.0
# via
# httpx
# openai
asgiref==3.3.2
# via django
asn1crypto==1.5.1
Expand Down Expand Up @@ -85,6 +88,8 @@ celery-redbeat==2.1.1
# via -r requirements.in
certifi==2019.11.28
# via
# httpcore
# httpx
# requests
# sentry-sdk
# snowflake-connector-python
Expand Down Expand Up @@ -138,6 +143,8 @@ defusedxml==0.6.0
# -r requirements.in
# python3-openid
# social-auth-core
distro==1.9.0
# via openai
dj-database-url==0.5.0
# via -r requirements.in
django==3.2.23
Expand Down Expand Up @@ -220,6 +227,8 @@ drf-extensions==0.7.0
# via -r requirements.in
drf-spectacular==0.27.1
# via -r requirements.in
exceptiongroup==1.2.0
# via anyio
filelock==3.12.0
# via snowflake-connector-python
frozenlist==1.3.0
Expand Down Expand Up @@ -277,16 +286,24 @@ grpcio-status==1.57.0
gunicorn==20.1.0
# via -r requirements.in
h11==0.13.0
# via wsproto
# via
# httpcore
# wsproto
hexbytes==1.0.0
# via dlt
hogql-parser==1.0.3
# via -r requirements.in
httpcore==1.0.2
# via httpx
httpx==0.26.0
# via openai
humanize==4.9.0
# via dlt
idna==2.8
# via
# -r requirements.in
# anyio
# httpx
# requests
# snowflake-connector-python
# trio
Expand Down Expand Up @@ -364,7 +381,7 @@ oauthlib==3.1.0
# via
# requests-oauthlib
# social-auth-core
openai==0.27.8
openai==1.10.0
# via -r requirements.in
openapi-schema-validator==0.6.2
# via openapi-spec-validator
Expand Down Expand Up @@ -441,7 +458,9 @@ pycparser==2.20
pycryptodomex==3.18.0
# via snowflake-connector-python
pydantic==2.5.3
# via -r requirements.in
# via
# -r requirements.in
# openai
pydantic-core==2.14.6
# via pydantic
pyjwt==2.4.0
Expand Down Expand Up @@ -513,7 +532,6 @@ requests==2.31.0
# google-cloud-bigquery
# infi-clickhouse-orm
# jsonschema-path
# openai
# posthoganalytics
# prance
# requests-oauthlib
Expand Down Expand Up @@ -570,7 +588,11 @@ slack-sdk==3.17.1
smmap==5.0.1
# via gitdb
sniffio==1.2.0
# via trio
# via
# anyio
# httpx
# openai
# trio
snowflake-connector-python==3.0.4
# via -r requirements.in
social-auth-app-django==5.0.0
Expand Down Expand Up @@ -629,7 +651,9 @@ types-setuptools==69.0.0.0
# via requirements-parser
typing-extensions==4.7.1
# via
# anyio
# dlt
# openai
# psycopg
# pydantic
# pydantic-core
Expand Down

0 comments on commit 974553f

Please sign in to comment.