-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugins.py
212 lines (173 loc) · 6.33 KB
/
plugins.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
import os
import uuid
import requests
import structlog
from litellm import embedding
logger = structlog.get_logger()
class Plugins:
"""
Plugins for quadlayer
We provide several plugins by default, but you can add your own plugins
by adding them to the plugins list.
"""
def __init__(self):
search_plugin = {
"name": "search",
"description": "Search using search engine, useful when requires recent knowledge",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Query to search",
},
},
"required": ["query"],
},
}
notes_plugin = [
{
"name": "create_note",
"description": "Create a note, returns note ID",
"parameters": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "Note title",
},
"content": {
"type": "string",
"description": "Note content",
},
},
"required": ["title", "content"],
},
},
{
"name": "retrieve_note",
"description": "Retrieve a note, returns note contents",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Query to search (e.g. note title or topic)",
},
},
"required": ["query"],
},
},
]
self.plugins = []
self.plugins_map = {}
self.bing_api_key = os.environ.get("BING_API_KEY")
self.use_chroma = os.environ.get("USE_CHROMA", "false").lower() == "true"
if self.bing_api_key:
logger.debug("Bing API key found, enabling search plugin")
self.plugins.append(search_plugin)
self.plugins_map["search"] = self.search
if self.use_chroma:
logger.debug("ChromaDB enabled, enabling notes plugin")
self.plugins.extend(notes_plugin)
self.plugins_map["create_note"] = self.create_note
self.plugins_map["retrieve_note"] = self.retrieve_note
import chromadb
self.chroma_client = chromadb.PersistentClient(path=".chroma")
def search(self, query: str) -> str:
"""
Search using Bing
:param query: Query
:return: Search result
"""
if not self.bing_api_key:
raise Exception("Bing API key not found")
url = "https://api.bing.microsoft.com/v7.0/search"
headers = {"Ocp-Apim-Subscription-Key": self.bing_api_key}
params = {"q": query}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
if not search_results["webPages"]["value"]:
return "No results found"
snippets = [
f"{result['name']}: {result['snippet']}"
for result in search_results["webPages"]["value"]
]
return "\n\n".join(snippets)
def wit(self, query: str, api_key: str | None = None) -> str:
"""
Get intent using wit.ai
:param query: Query
:param api_key: Wit API key
:return: Intent
"""
if not api_key:
raise Exception("Wit API key not found")
response = requests.get(
"https://api.wit.ai/message",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
params={"q": query},
)
response.raise_for_status()
wit_response = response.json()
return wit_response["intents"][0]["name"]
def create_note(self, title: str, content: str, **kwargs) -> str:
"""
Create a note
:param title: Note title
:param content: Note content
:return: Note ID
"""
collection = self.chroma_client.get_or_create_collection(kwargs["user_id"])
# Let's generate embeddings for the title + content
embeddings_response = embedding(
os.environ.get("EMBEDDINGS_MODEL"),
input=[f"{title}\n\n{content}"],
)
# Let's generate note ID
note_id = str(uuid.uuid4())
# And store the note
collection.add(
embeddings=[embeddings_response["data"][0]["embedding"]],
documents=[f"{title}\n\n{content}"],
ids=[note_id],
)
return note_id
def retrieve_note(self, query: str, **kwargs) -> str:
"""
Retrieve a note
:param query: Query to search (e.g. note title or topic)
:return: Note content
"""
collection = self.chroma_client.get_or_create_collection(kwargs["user_id"])
# Let's generate embeddings for the query
embeddings_response = embedding(
os.environ.get("EMBEDDINGS_MODEL"),
input=[query],
)
# And search for the note
search_response = collection.query(
query_embeddings=[embeddings_response["data"][0]["embedding"]],
n_results=3,
)
search_response = [
f"--BEGIN NOTE--\n{document}\n--END NOTE--"
for document in search_response["documents"]
]
return "\n\n".join(search_response)
def call_function(self, name: str, arguments: dict, user_id: str) -> str:
"""
Call a function
:param name: Function name
:param arguments: Function arguments
:return: Function result
"""
logger.debug("Calling function", name=name, arguments=arguments)
if name not in self.plugins_map:
raise Exception(f"Function {name} not found")
arguments["user_id"] = user_id
return str(self.plugins_map[name](**arguments))