-
Notifications
You must be signed in to change notification settings - Fork 8
/
ai.py
101 lines (75 loc) · 2.99 KB
/
ai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import json
import openai
from dotenv import load_dotenv
from pipeline import MaxPipeline
from plugins.pagerduty import current_oncalls
load_dotenv()
# OPENAI_MODEL = "gpt-3.5-turbo"
OPENAI_MODEL = "gpt-4"
OPENAI_TOKEN = os.environ.get("OPENAI_TOKEN")
if not OPENAI_TOKEN:
print("Please set OPENAI_TOKEN in your environment variables.")
exit()
## Initialize OpenAI
openai.api_key = OPENAI_TOKEN
oncalls = ""
def update_oncalls():
print("updating oncalls")
global oncalls
oncalls = current_oncalls()
return oncalls
pipeline = MaxPipeline(openai_token=OPENAI_TOKEN)
async def ai_chat_thread(thread):
documents = pipeline.retrieve_context(thread[0]["content"])
json_docs = json.dumps(
[
{"page_content": doc.page_content, "metadata": doc.metadata}
for doc in documents
],
indent=2,
)
print(json_docs)
SYSTEM_PROMPT = """
You are the trusty PostHog support AI named Max. You are also PostHog's Mascot!
Please continue the conversation in a way that is helpful to the user and also makes the user feel like they are talking to a human.
Only suggest using PostHog and ClickHouse products or services. HogQL is the same as ClickHouse SQL. Do not suggest products or services from other companies.
Please answer the question according to the following context.
Do not create links. Only reference the source from the metadata.source object in the context.
If you get a question about pricing please refer to the reasonable and transparent pricing on the pricing page at https://posthog.com/pricing.
If you are unsure of the answer, please say "I'm not sure" and encourage the user to ask PostHog staff.
Try not to mention <@*> in the response.
If you are asked about hoge, please respond with just "We don't talk about Hoge."
Do not put the Disclaimer in your response. It will be added automatically.
"""
CONTEXT_PROMPT = f"""
Context:
{json_docs}
---
Now answer the following question:
"""
first_message = thread[0]
follow_up_thread = thread[1:]
prompt = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": CONTEXT_PROMPT + first_message["content"]},
*follow_up_thread,
]
completion = openai.ChatCompletion.create(model=OPENAI_MODEL, messages=prompt)
completion = completion.choices[0].message.content
sources = [
"https://github.com/PostHog/posthog.com/blob/master/" + doc.metadata["source"]
for doc in documents
]
sources = "\n".join(sources)
disclaimer = "<https://github.com/PostHog/max-ai#disclaimer|Disclaimer> :love-hog:"
response = f"""{completion}
{disclaimer}
"""
return response
async def summarize_thread(thread):
prompt = f"""Summarize this: {thread}"""
completion = openai.ChatCompletion.create(
model=OPENAI_MODEL, messages=[{"role": "user", "content": prompt}]
)
return completion.choices[0].message.content