-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathconversation_streaming.py
More file actions
131 lines (109 loc) · 3.43 KB
/
conversation_streaming.py
File metadata and controls
131 lines (109 loc) · 3.43 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from operator import truediv
import os
import sys
curr_dir = os.path.dirname(os.path.realpath(__file__))
repo_root = os.path.abspath(os.path.join(curr_dir, os.pardir))
sys.path.insert(1, os.path.join(repo_root, "src"))
import typesense
from typesense.types.document import MessageChunk, StreamConfigBuilder
def require_env(name: str) -> str:
value = os.environ.get(name)
if not value:
raise RuntimeError(f"Missing required environment variable: {name}")
return value
typesense_api_key = require_env("TYPESENSE_API_KEY")
openai_api_key = require_env("OPENAI_API_KEY")
client = typesense.Client(
{
"api_key": typesense_api_key,
"nodes": [
{
"host": "localhost",
"port": "8108",
"protocol": "http",
}
],
"connection_timeout_seconds": 10,
}
)
try:
client.conversations_models["conv-model-1"].delete()
except Exception:
pass
try:
client.collections["streaming_docs"].delete()
except Exception:
pass
try:
client.collections["conversation_store"].delete()
except Exception:
pass
client.collections.create(
{
"name": "conversation_store",
"fields": [
{"name": "conversation_id", "type": "string"},
{"name": "model_id", "type": "string"},
{"name": "timestamp", "type": "int32"},
{"name": "role", "type": "string", "index": False},
{"name": "message", "type": "string", "index": False},
],
}
)
client.collections.create(
{
"name": "streaming_docs",
"fields": [
{"name": "title", "type": "string"},
{
"name": "embedding",
"type": "float[]",
"embed": {
"from": ["title"],
"model_config": {
"model_name": "openai/text-embedding-3-small",
"api_key": openai_api_key,
},
},
},
],
}
)
client.collections["streaming_docs"].documents.create(
{"id": "stream-1", "title": "Company profile: a developer tools firm."}
)
client.collections["streaming_docs"].documents.create(
{"id": "stream-2", "title": "Internal memo about a quarterly planning meeting."}
)
conversation_model = client.conversations_models.create(
{
"id": "conv-model-1",
"model_name": "openai/gpt-3.5-turbo",
"history_collection": "conversation_store",
"api_key": openai_api_key,
"system_prompt": (
"You are an assistant for question-answering. "
"Only use the provided context. Add some fluff about you Being an assistant built for Typesense Conversational Search and a brief overview of how it works"
),
"max_bytes": 16384,
}
)
stream = StreamConfigBuilder()
@stream.on_chunk
def on_chunk(chunk: MessageChunk) -> None:
print(chunk["message"], end="", flush=True)
@stream.on_complete
def on_complete(response: dict) -> None:
print("\n---\nComplete response keys:", response.keys())
client.collections["streaming_docs"].documents.search(
{
"q": "What is this document about?",
"query_by": "embedding",
"exclude_fields": "embedding",
"conversation": True,
"prefix": False,
"conversation_stream": True,
"conversation_model_id": conversation_model["id"],
"stream_config": stream,
}
)