-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathchain_store.py
More file actions
173 lines (152 loc) · 6.14 KB
/
chain_store.py
File metadata and controls
173 lines (152 loc) · 6.14 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
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
import json
import os
import requests
import numpy as np
from typing import List, Dict, Optional
from datetime import datetime
def init_store(store_file: str = "successful_chains.json") -> None:
"""Initialize the chain store if it doesn't exist."""
if not os.path.exists(store_file):
with open(store_file, 'w') as f:
json.dump({"chains": []}, f)
def get_embedding(text: str, cohere_api_key: str, input_type: str = "search_document") -> Optional[List[float]]:
"""Get embeddings from Cohere API."""
try:
response = requests.post(
"https://api.cohere.ai/v1/embed",
headers={
"Authorization": f"Bearer {cohere_api_key}",
"Content-Type": "application/json"
},
json={
"texts": [text],
"model": "embed-english-v3.0",
"input_type": input_type,
"embedding_type": "float"
}
)
response.raise_for_status()
return response.json()["embeddings"][0]
except Exception as e:
print(f"Error getting embedding: {e}")
return None
def cosine_similarity(a: List[float], b: List[float]) -> float:
"""Calculate cosine similarity between two vectors."""
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
def save_successful_chain(
task: str,
conversation_history: List[Dict],
final_response: str,
cohere_api_key: str,
thinking_tools: List[Dict],
output_tools: List[Dict],
metadata: Dict,
store_file: str = "successful_chains.json"
) -> bool:
"""Save a successful chain to the store."""
try:
# Get embedding for the task
embedding = get_embedding(task, cohere_api_key)
if not embedding:
return False
# Initialize store if it doesn't exist
if not os.path.exists(store_file):
store = {"chains": []}
else:
try:
with open(store_file, 'r') as f:
store = json.load(f)
except json.JSONDecodeError:
# If file exists but is invalid JSON, start fresh
store = {"chains": []}
# Process conversation history to redact long tool responses
processed_history = []
for msg in conversation_history:
if msg['role'] == 'tool' and len(msg['content']) > 1500:
msg = msg.copy() # Create a copy to avoid modifying the original
msg['content'] = "[redacted for token savings]"
processed_history.append(msg)
# Add new chain
chain = {
"task": task,
"embedding": embedding,
"conversation_history": processed_history,
"final_response": final_response,
"thinking_tools": thinking_tools,
"output_tools": output_tools,
"timestamp": datetime.now().isoformat(),
"metadata": metadata
}
store["chains"].append(chain)
# Save updated store
with open(store_file, 'w') as f:
json.dump(store, f, indent=2)
return True
except Exception as e:
print(f"Error saving chain: {str(e)}") # More detailed error message
return False
def get_similar_chains(
task: str,
cohere_api_key: str,
n: int = 3,
store_file: str = "successful_chains.json"
) -> List[Dict]:
"""Get n most similar chains for a given task."""
try:
# Get embedding for the query task
query_embedding = get_embedding(task, cohere_api_key, input_type="search_query")
if not query_embedding:
return []
# Load chains
with open(store_file, 'r') as f:
store = json.load(f)
# Calculate similarities
similarities = []
for chain in store["chains"]:
similarity = cosine_similarity(query_embedding, chain["embedding"])
similarities.append((similarity, chain))
# Sort by similarity and get top n
similarities.sort(reverse=True, key=lambda x: x[0])
result = [chain for _, chain in similarities[:n]]
return result
except Exception as e:
return []
def prepare_examples_messages(similar_chains: List[Dict], current_tools: List[Dict]) -> List[Dict]:
"""
Prepare example chains as messages for the prompt.
Now includes information about available tools.
"""
if not similar_chains:
return []
messages = []
for chain in similar_chains:
# Get the tool names for both current and historical tools
current_tool_names = {t['function']['name'] for t in current_tools}
historical_tool_names = {t['function']['name'] for t in chain.get('tools', [])}
# Create tool availability message
tool_message = "Available tools in this example:"
for tool_name in historical_tool_names:
status = "✓" if tool_name in current_tool_names else "✗"
tool_message += f"\n- {tool_name} {status}"
# Add system message with the example task and tool information
messages.append({
"role": "system",
"content": (
"<TASK>\n"
f"{chain['task']}\n\n"
f"{tool_message}\n\n"
"<INSTRUCTIONS>\n"
"Slow down your thinking by breaking complex questions into multiple reasoning steps.\n"
"Each individual reasoning step should be brief.\n"
"Return <DONE> after the last step."
)
})
# Add the conversation history
messages.extend(chain["conversation_history"])
# For each message, replace any instance of the substring TASK with EXAMPLE_TASK
for i, msg in enumerate(messages):
if 'TASK' in msg['content']:
messages[i]['content'] = msg['content'].replace('CURRENT_TASK', 'EXAMPLE_TASK')
messages[i]['content'] = msg['content'].replace('TASK', 'EXAMPLE_TASK')
messages[i]['content'] = messages[i]['content'].replace('EXAMPLE_EXAMPLE_TASK', 'EXAMPLE_TASK')
return messages