This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
172 lines (146 loc) · 5.01 KB
/
helpers.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File: ./helpers.py
from enum import Enum
from datasets import load_dataset
from datetime import datetime
from dateutil import parser
from typing import Dict, Union, List, Any, Literal, Optional
from collections.abc import Iterator
import claudette
from anthropic.types import Message
import ollama
import subprocess
import weaviate
from weaviate import WeaviateClient
from weaviate.collections import Collection
from weaviate.classes.query import Metrics, Filter
import os
class CollectionName(str, Enum):
"""Enum for Weaviate collection names."""
SUPPORTCHAT = "SupportChat"
SUPPORTCHAT_MT = "SupportChatMT"
def connect_to_weaviate() -> WeaviateClient:
client = weaviate.connect_to_local(
port=8080,
# Passing headers in case we use these integrations
headers={
"X-ANTHROPIC-API-KEY": os.environ["ANTHROPIC_API_KEY"],
"X-OPENAI-API-KEY": os.environ["OPENAI_API_KEY"],
"X-COHERE-API-KEY": os.environ["COHERE_API_KEY"],
},
)
return client
def connect_to_mt_weaviate() -> WeaviateClient:
client = weaviate.connect_to_local(
port=8180,
grpc_port=50151,
# Passing headers in case we use these integrations
headers={
"X-ANTHROPIC-API-KEY": os.environ["ANTHROPIC_API_KEY"],
"X-OPENAI-API-KEY": os.environ["OPENAI_API_KEY"],
"X-COHERE-API-KEY": os.environ["COHERE_API_KEY"],
},
)
return client
def get_collection_names() -> List[str]:
client = connect_to_weaviate()
collections = client.collections.list_all(simple=True)
return collections.keys()
def _parse_time(time_string: str) -> datetime:
# Parse the string into a datetime object
dt = parser.parse(time_string)
return dt
def get_data_objects(
max_text_length: int = 10**5,
) -> Iterator[Dict[str, Union[datetime, str, int]]]:
ds = load_dataset("Rakuto/twitter_customer_support_dialogue")["train"]
for item in ds:
yield {
"text": item["text"][:max_text_length],
"dialogue_id": item["dialogue_id"],
"company_author": item["company_author"],
"created_at": _parse_time(item["created_at"]),
}
def get_top_companies(collection: Collection):
response = collection.aggregate.over_all(
return_metrics=Metrics("company_author").text(
top_occurrences_count=True, top_occurrences_value=True, count=True
)
)
return response.properties["company_author"].top_occurrences
def weaviate_query(
collection: Collection,
query: str,
company_filter: str,
limit: int,
search_type: Literal["Hybrid", "Vector", "Keyword"],
rag_query: Optional[str] = None,
):
# ================================================================================
# STUDENT **TODO**:
# Implement the `weaviate_query` function to query Weaviate.
# ================================================================================
if company_filter:
# What does a filter look like for the company_author property?
company_filter_obj = None
else:
company_filter_obj = None
# What should alpha be for a hybrid, vector, or keyword search?
if search_type == "Hybrid":
alpha = 0
elif search_type == "Vector":
alpha = 0
elif search_type == "Keyword":
alpha = 0
if not rag_query:
# Implement the search query
search_response = None
else:
# Implement the RAG query
search_response = None
# ================================================================================
# If you need help with the query, check the hints/helpers.py file.
# ================================================================================
return search_response
def get_pprof_results(port=6060) -> str:
return subprocess.run(
["go", "tool", "pprof", "-top", f"http://localhost:{port}/debug/pprof/heap"],
capture_output=True,
text=True,
timeout=10,
)
def manual_rag(
rag_query: str, context: str, provider: Literal["claude", "ollama"]
) -> List[str]:
prompt = f"""
Answer this query <query>{rag_query}</query>
about these conversations between
customer support people and customers: {context}
"""
if provider == "claude":
chat = claudette.Chat(
model="claude-3-haiku-20240307" # e.g. "claude-3-haiku-20240307" or "claude-3-5-sonnet-20240620"
)
r: Message = chat(prompt)
rag_responses = [c.text for c in r.content]
return rag_responses
elif provider == "ollama":
response = ollama.chat(
model="gemma2b:2b",
messages=[
{
"role": "user",
"content": prompt,
},
],
)
return [(response["message"]["content"])]
STREAMLIT_STYLING = """
<style>
.stHeader {
background-color: #f0f2f6;
padding: 1.5rem;
border-radius: 10px;
margin-bottom: 2rem;
}
</style>
"""