-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
224 lines (198 loc) · 7.31 KB
/
utils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'''
Author: Pengzirong [email protected]
Date: 2024-09-06 14:21:58
LastEditors: Pengzirong
LastEditTime: 2024-09-12 15:29:30
Description: file content
'''
# from fetch_langfuse import FetchLangfuse
import aiohttp
import os
def process_llm_batch(llm_observations):
"""Process a batch of LLM observations.
Args:
llm_observations (list): A list of LLM observations.
Returns:
dict: A dictionary containing the processed evaluation batch.
"""
evaluation_batch = {
"question": [],
"contexts": [],
"answer": [],
"trace_id": [],
"observation_id": []
}
for observation in llm_observations:
d = dict(observation)
question = ""
context = []
for item in d["input"]:
if item["role"] == "user":
question = item["content"]
elif item["role"] == "system":
context.append(item["content"])
evaluation_batch["question"].append(question)
evaluation_batch["contexts"].append(context)
if "output" in d and "text" in d["output"]:
evaluation_batch["answer"].append(d["output"]["text"])
evaluation_batch["trace_id"].append(observation["traceId"])
evaluation_batch["observation_id"].append(observation["id"])
return evaluation_batch
def process_retrieval_batch(retrieval_observations):
"""Process a batch of retrieval observations.
Args:
retrieval_observations (list): A list of retrieval observations.
Returns:
dict: A dictionary containing the processed evaluation batch.
"""
evaluation_batch = {
"query": [],
"retrieval result": [],
"trace_id": [],
"observation_id": []
}
for observation in retrieval_observations:
d = dict(observation)
query = d["input"]["query"]
retrieval_result = {"title": [], "content": []}
evaluation_batch["query"].append(query)
if "output" in d:
for item in d["output"]["result"]:
if "title" in item and "content" in item:
retrieval_result["title"].append(item["title"])
retrieval_result["content"].append(item["content"])
evaluation_batch["retrieval result"].append(retrieval_result)
evaluation_batch["trace_id"].append(observation["traceId"])
evaluation_batch["observation_id"].append(observation["id"])
return evaluation_batch
def pull_scores_to_langfuse(langfuse, scores, scores_keys, node_name=None):
"""Pull scores to Langfuse.
Args:
langfuse (Langfuse): The Langfuse object.
scores (dict): The scores dictionary.
scores_keys (list): The list of keys for evaluation.
"""
from datetime import datetime
for _, row in scores.iterrows():
for key in scores_keys:
trace_id = row['trace_id']
observation_id = row['observation_id']
name = f"{key}-{node_name}" if node_name else key
score = row[key]
langfuse.score(
id=f"{trace_id}-{observation_id}-{name}",
trace_id = trace_id,
observation_id=observation_id,
name=name,
value=score,
comment=f"Last updated at {datetime.now().strftime('%Y/%m/%d %H:%M:%S')}"
)
def pull_score_to_langfuse(langfuse, score, trace_id, observation_id, name):
"""Pull a single score to Langfuse.
Args:
langfuse (Langfuse): The Langfuse object.
score (float): The score.
trace_id (str): The trace ID.
observation_id (str): The observation ID.
name (str): The name of the score.
"""
from datetime import datetime
langfuse.score(
id=f"{trace_id}-{observation_id}-{name}",
trace_id = trace_id,
observation_id=observation_id,
name=name,
value=score,
comment=f"Last updated at {datetime.now().strftime('%Y/%m/%d %H:%M:%S')}"
)
async def async_pull_score_to_langfuse(score, trace_id, observation_id, name):
"""Pull a single score to Langfuse asynchronously.
Args:
score (float): The score.
trace_id (str): The trace ID.
observation_id (str): The observation ID.
name (str): The name of the score.
"""
from datetime import datetime
url = f"{os.getenv('LANGFUSE_HOST')}/api/score"
screct_key = os.getenv('LANGFUSE_SECRET_KEY')
public_key = os.getenv('LANGFUSE_PUBLIC_KEY')
headers = {
"Content-Type": "application/json"
}
payload = {
"id": f"{trace_id}-{observation_id}-{name}",
"traceId": trace_id,
"name": name,
"value": score,
"observationId": observation_id,
"comment": f"Last updated at {datetime.now().strftime('%Y/%m/%d %H:%M:%S')}",
}
async with aiohttp.ClientSession() as session:
async with session.post(url, auth=aiohttp.BasicAuth(public_key, screct_key), headers=headers, data=json.dumps(payload)) as response:
return await response.text()
async def send_chat_message(
url,
api_key,
query: str,
inputs={},
response_mode: ["streaming", "blocking"] = "blocking",
user: str = "abc-123",
file_array = []
):
"""Send a chat message.
Args:
url (str): The URL of the chat message API.
api_key (str): The API key for authentication.
query (str): The chat message query.
inputs (dict, optional): Additional inputs for the chat message. Defaults to {}.
response_mode (str, optional): The response mode for the chat message. Defaults to "blocking".
user (str, optional): The user identifier. Defaults to "abc-123".
file_array (list, optional): An array of files to be sent with the chat message. Defaults to [].
Returns:
dict: The response from the chat message API.
"""
base_url = f"{url}/chat-messages"
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
payload = {
"inputs": inputs,
"query": query,
"response_mode": response_mode,
"conversation_id": "",
"user": user,
"files": file_array
}
async with aiohttp.ClientSession() as session:
async with session.post(base_url, headers=headers, json=payload) as response:
return await response.json()
def get_ragas_llm_and_embeddings():
"""Get Ragas LLM and Embeddings.
Returns:
tuple: A tuple containing the LLM and embeddings objects.
"""
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
from langchain_openai.chat_models import ChatOpenAI
from langchain_openai.embeddings import OpenAIEmbeddings
llm = LangchainLLMWrapper(
ChatOpenAI(
model=os.getenv("RAGAS_CRITIC_LLM"),
openai_api_base=os.getenv("RAGAS_BASE_URL"),
openai_api_key=os.getenv("RAGAS_API_KEY"),
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
)
embeddings = LangchainEmbeddingsWrapper(
OpenAIEmbeddings(
model=os.getenv("RAGAS_EMBEDDING"),
base_url=os.getenv("RAGAS_BASE_URL"),
api_key=os.getenv("RAGAS_API_KEY"),
)
)
return llm, embeddings