-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
97 lines (89 loc) · 4.09 KB
/
tools.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
import json
from dotenv import load_dotenv
from langchain_core.documents import Document
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain.tools import BaseTool, StructuredTool, tool
from langchain.chains.query_constructor.schema import AttributeInfo
load_dotenv()
# Create knowledge of app functions
docs = [
Document(
page_content="""
An app that aims to enhance spatial navigation for visually impaired individuals by utilizing an intelligent AI-driven system that aids in grocery shopping.
At the core of this is a bracelet equipped with vibration feedback. By varying the intensity of the vibration, the app guides users to products in a supermarket.
The app is designed to interface with the bracelet. The goal is to empower blind users with greater independence and ease in their supermarket shopping experiences.
""",
metadata={"app_id": 1, "name": "Spatial Navigator"},
),
Document(
page_content="""
A list where a user can add and remove products from that they want to aquire at a supermarket.
The app will send you the current grocery list as additional data.
Return the product(s) that need to be added, appended with either + (to add) or - (to remove), including the amount.
Example: [+4]Jonagold Apple; [-1]Quaker Oats""",
metadata={"app_id": 1, "type": "function", "label": "grocery-list", "additional-data-required": True},
),
Document(
page_content="Enables the user to scan barcodes of products to gain detailed nutritional information about them.",
metadata={"app_id": 1, "label": "barcode-scanner", "additional-data-required": False},
),
Document(
page_content="""
Allows the user to save their favorite supermarkets and start GPS navigation to this supermarket.
Extra Feature: If the user mentions a name of a supermarket, return the name of the supermarket.""",
metadata={"app_id": 1, "label": "navigation", "additional-data-required": False},
),
Document(
page_content="Can identify products in a supermarket and hands of the user in real time to help find the right products.",
metadata={"app_id": 1, "label": "object-and-hand-recognition", "additional-data-required": False},
),
]
additional_data_descriptions = {
"app_id": 1,
"label": "grocery-list",
"instruction": """
Extra Feature: Return the product(s) that need to be added, appended with either + (to add) or - (to remove), including the amount.
Example: [+4]Jonagold Apple; [-1]Quaker Oats"""
}
# metadata_field_info = [
# AttributeInfo(
# name="type",
# description="Whether this document describes the app itself or a functionality within the app.",
# type="string",
# ),
# AttributeInfo(
# name="name",
# description="The name of the app.",
# type="string",
# ),
# AttributeInfo(
# name="app_id",
# description="The unique identifyer of the app. Connects app descriptions to app functionality.",
# type="integer",
# ),
# AttributeInfo(
# name="label",
# description="The unique label of a function within an app.",
# type="string",
# )
# ]
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = Chroma.from_documents(docs, embeddings)
app_descriptions_retriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 3, "filter": {"type": "app"}},
)
app_functions_retriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 3, "filter": {"type": "function"}},
)
@tool
def app_functions(query: str) -> str:
"""Gives the most relevant app functions including descriptions of it's purpose and accompanying metadata based on the input string."""
docsList = app_functions_retriever.invoke(query)
strList =[]
# Export the document content as well as its metadata
for doc in docsList:
strList.append({"document" : doc.page_content ,"metadata" : doc.metadata})
return json.dumps(strList)