File size: 12,572 Bytes
ddba08c 52237c6 ddba08c 0c8d19f 5dce464 0c8d19f 0e1166a 1d4199f 0e1166a ddba08c 52237c6 ddba08c 52237c6 ddba08c 52237c6 0e1166a 52237c6 ddba08c 52237c6 ddba08c 52237c6 0c8d19f 52237c6 0c8d19f 0e1166a 5dce464 52237c6 ddba08c 52237c6 ddba08c 52237c6 0c8d19f 0e1166a 0c8d19f 5dce464 0e1166a 5dce464 1d4199f 0e1166a 1d4199f 0e1166a ddba08c 0e1166a ddba08c 1d4199f 0e1166a ddba08c 0c8d19f ddba08c 52237c6 ddba08c 52237c6 0c8d19f 52237c6 ddba08c 0c8d19f 5dce464 0c8d19f 0e1166a 5dce464 0e1166a 1d4199f 0e1166a 1d4199f |
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import os
from dotenv import load_dotenv
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_openai import AzureChatOpenAI
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition, ToolNode
from langchain_core.runnables import RunnableConfig
# Wikepedia tool
from langchain_community.document_loaders import WikipediaLoader
# Tavily web search
from langchain_community.tools.tavily_search import TavilySearchResults
# Python loader
from langchain_community.document_loaders import PythonLoader
# Whisper
from langchain_community.document_loaders.parsers.audio import AzureOpenAIWhisperParser
from langchain_core.documents.base import Blob
# excel
from langchain_community.document_loaders import UnstructuredExcelLoader
load_dotenv()
# --- TOOLS ---
# Simple tool to test a tool call
def meaning_of_life(a: int, b: int) -> int:
"""Returns meaning of life
Args:
a: first int
b: second int
"""
return 42
# https://www.restack.io/docs/langchain-knowledge-wikipedia-loader-cat-ai
# https://api.python.langchain.com/en/latest/community/document_loaders/langchain_community.document_loaders.wikipedia.WikipediaLoader.html#
# ¤ I ended up not using this tool since I could not get it to return the table data in the Markov question. The Taveli search tool also find wiki content
# Better approach could be to combine this tool (to get URL) + a webreader to get content
def wikipedia_search(query: str) -> str:
"""Searches Wikipedia for a given query and fetches full document
Args:
query: the query to search for
"""
loader = loader = WikipediaLoader(
query=query,
load_max_docs=1,
doc_content_chars_max=4000,
load_all_available_meta=False,
)
documents = loader.load()
formatted_search_docs = "\n\n---\n\n"
for next_doc in documents:
formatted_doc = f'<Document source="{next_doc.metadata["source"]}" title="{next_doc.metadata.get("title", "")}"\n{next_doc.page_content}\n</Document>'
formatted_search_docs = formatted_search_docs + formatted_doc
result = f"{{wiki_results: {formatted_search_docs}}}"
return result
def web_search(query: str) -> str:
"""Search Web with Tavily for a query and return results.
Args:
query: The search query."""
tool = TavilySearchResults(
max_results=3,
include_answer=True,
include_raw_content=True,
include_images=True,
# search_depth="advanced",
# include_domains = []
# exclude_domains = []
)
documents = tool.invoke(input=query)
formatted_search_docs = "\n\n---\n\n"
for next_doc in documents:
url = next_doc["url"]
title = next_doc["title"]
content = next_doc["content"]
formatted_doc = (
f'<Document source="{url}" title="{title}"\n{content}\n</Document>'
)
formatted_search_docs = formatted_search_docs + formatted_doc
result = f"{{web_results: {formatted_search_docs}}}"
return result
# https://python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.python.PythonLoader.html
def python_file_reader(file_name: str) -> str:
"""Reads a python file and returns the content
Args:
file_name: the filename to read
"""
file_path = os.path.join(os.path.dirname(__file__), "files", file_name)
loader = PythonLoader(file_path=file_path)
documents = loader.load()
formatted_search_docs = "\n\n---\n\n"
for next_doc in documents:
formatted_doc = (
f'<Document source="{file_name}"\n{next_doc.page_content}\n</Document>'
)
formatted_search_docs = formatted_search_docs + formatted_doc
result = f"{{python_code: {formatted_search_docs}}}"
return result
# https://python.langchain.com/docs/integrations/document_loaders/microsoft_excel/
# https://python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.excel.UnstructuredExcelLoader.html
def excel_file_reader(excel_file_name: str) -> str:
"""Reads an excel file and returns the content
Args:
excel_file_name: the filename to read
"""
file_path = os.path.join(os.path.dirname(__file__), "files", excel_file_name)
loader = UnstructuredExcelLoader(file_path, mode="elements")
documents = loader.load()
formatted_search_docs = "\n\n---\n\n"
for next_doc in documents:
formatted_doc = f'<Document source="{excel_file_name}"\n{next_doc.metadata["text_as_html"]}\n</Document>'
formatted_search_docs = formatted_search_docs + formatted_doc
result = f"{{python_code: {formatted_search_docs}}}"
return result
# https://python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.parsers.audio.AzureOpenAIWhisperParser.html
def audio_to_text(audio_file_name: str) -> str:
"""Listen to audio and extract text from speech
Args:
audio_file_name: the audio filename to read
"""
file_path = os.path.join(os.path.dirname(__file__), "files", audio_file_name)
deployment_name = os.environ.get("AZURE_WHISPER_DEPLOYMENT")
api_version = os.environ.get("AZURE_WHISPER_API_VERSION")
api_key = os.environ.get("AZURE_WHISPER_API_KEY")
azure_endpoint = os.environ.get("AZURE_WHISPER_ENDPOINT")
whisper_parser = AzureOpenAIWhisperParser(
deployment_name=deployment_name,
api_version=api_version,
api_key=api_key,
azure_endpoint=azure_endpoint,
# other params...
)
audio_blob = Blob(path=file_path)
response = whisper_parser.parse(audio_blob)
formatted_search_docs = "\n\n---\n\n"
for next_doc in response:
formatted_doc = f'<Document source="{audio_file_name}"\n{next_doc.page_content}\n</Document>'
formatted_search_docs = formatted_search_docs + formatted_doc
result = f"{{transscribed_audio: {formatted_search_docs}}}"
return result
tools = [
meaning_of_life,
web_search,
python_file_reader,
audio_to_text,
wikipedia_search,
excel_file_reader,
]
# --- GRAPH ---
# This functions allow us to use the web interface to test the graph
def make_graph(config: RunnableConfig):
graph = create_graph()
return graph
# This function is used to create the graph
def create_graph():
# Define LLM with bound tools
azure_endpoint = os.environ.get("AZURE_ENDPOINT_LLM")
api_key = os.environ.get("AZURE_API_KEY_LLM")
api_version = os.environ.get("AZURE_API_VERSION_LLM")
deployment = os.environ.get("AZURE_DEPLOYMENT_LLM")
# Initialize LLM
llm = AzureChatOpenAI(
azure_deployment=deployment,
api_version=api_version,
temperature=0.01,
max_tokens=None,
timeout=None,
max_retries=2,
api_key=api_key,
azure_endpoint=azure_endpoint,
)
llm_with_tools = llm.bind_tools(tools)
# System message
# original_system_prompt_txt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
system_prompt_txt = "You are a general AI assistant that uses tools to answer questions. YOUR FINAL ANSWER should be a number represented as digits OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number or how many, only reply with a number represented as digits nothing else, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for an abbreviation or a code only reply with that. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
sys_msg = SystemMessage(system_prompt_txt)
# Node
def assistant(state: MessagesState):
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
# Build graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
# If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
tools_condition,
)
builder.add_edge("tools", "assistant")
# Compile graph
graph = builder.compile()
return graph
# You can run this script directly to test the graph
# Alternatively in a commandprompt run "langgraph dev" and it will allow you to interact with the graph in a web ui
if __name__ == "__main__":
# Build the graph
graph = create_graph()
# Run the graph
"""
print(f"******** TEST NORMAL LLM CALL ********")
question = "What is an elephant? "
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
print(f"******** TESTING MEANING OF LIFE TOOL ********")
question = "What is meaning of life 10+10?"
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
print("******** TESTING WIKEPEDIA TOOL ********")
# expected answer is "Samuel"
question = "Search Wikipedia and find out who is the recipient of the malko competition in 2024"
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
print("******** TESTING WEB SEARCH TOOL ********")
# expected answer is "Samuel"
question = "Search web for information about mozart"
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
print("******** PYTHON LOAD TOOL ********")
question = "what does this python code do? filename is f918266a-b3e0-4914-865d-4faa564f1aef.py"
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
print("******** TRANSSCRIBE AUDIO TOOL ********")
question = "Hi, I was out sick from my classes on Friday, so I'm trying to figure out what I need to study for my Calculus mid-term next week. My friend from class sent me an audio recording of Professor Willowbrook giving out the recommended reading for the test, but my headphones are broken :( Could you please listen to the recording for me and tell me the page numbers I'm supposed to go over? I've attached a file called Homework.mp3 that has the recording. Please provide just the page numbers as a comma-delimited list. And please provide the list in ascending order. File to use is 1f975693-876d-457b-a649-393859e79bf3.mp3"
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
print("******** EXCEL TOOL ********")
question = "The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places. File to use is 7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx"
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print()
"""
|