-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
80 lines (59 loc) · 2.31 KB
/
tools.py
File metadata and controls
80 lines (59 loc) · 2.31 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
import math
import re
import numexpr
from langchain_chroma import Chroma
from langchain_core.tools import BaseTool, tool
from langchain_openai import OpenAIEmbeddings
def calculator_func(expression: str) -> str:
"""Calculates a math expression using numexpr.
Useful for when you need to answer questions about math using numexpr.
This tool is only for math questions and nothing else. Only input
math expressions.
Args:
expression (str): A valid numexpr formatted math expression.
Returns:
str: The result of the math expression.
"""
try:
local_dict = {"pi": math.pi, "e": math.e}
output = str(
numexpr.evaluate(
expression.strip(),
global_dict={}, # restrict access to globals
local_dict=local_dict, # add common mathematical functions
)
)
return re.sub(r"^\[|\]$", "", output)
except Exception as e:
raise ValueError(
f'calculator("{expression}") raised error: {e}.'
" Please try again with a valid numerical expression"
)
calculator: BaseTool = tool(calculator_func)
calculator.name = "Calculator"
# Format retrieved documents
def format_contexts(docs):
return "\n\n".join(doc.page_content for doc in docs)
def load_chroma_db():
# Create the embedding function for our project description database
try:
embeddings = OpenAIEmbeddings()
except Exception as e:
raise RuntimeError(
"Failed to initialize OpenAIEmbeddings. Ensure the OpenAI API key is set."
) from e
# Load the stored vector database
chroma_db = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
retriever = chroma_db.as_retriever(search_kwargs={"k": 5})
return retriever
def database_search_func(query: str) -> str:
"""Searches chroma_db for information in the company's handbook."""
# Get the chroma retriever
retriever = load_chroma_db()
# Search the database for relevant documents
documents = retriever.invoke(query)
# Format the documents into a string
context_str = format_contexts(documents)
return context_str
database_search: BaseTool = tool(database_search_func)
database_search.name = "Database_Search" # Update name with the purpose of your database