File size: 7,291 Bytes
ccc745c 1daff82 ccc745c 1daff82 6ac8934 1daff82 6ac8934 1daff82 6ac8934 7df20a1 1daff82 7df20a1 6ac8934 1daff82 7df20a1 6ac8934 1daff82 e0f29bb 1daff82 e0f29bb 6ac8934 1daff82 7df20a1 e0f29bb 1daff82 e0f29bb 1daff82 e0f29bb 1daff82 de718ca 1daff82 e0f29bb 1daff82 e0f29bb 1daff82 e0f29bb 1daff82 54c62fb e0f29bb 7df20a1 1daff82 6ac8934 e0f29bb 1daff82 e0f29bb 6d24d35 1daff82 |
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 |
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_tavily import TavilyExtract
from youtube_transcript_api import YouTubeTranscriptApi
from langchain_core.messages import SystemMessage, HumanMessage
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import ToolNode
from langgraph.prebuilt import tools_condition
import base64
import httpx
load_dotenv()
@tool
def add(a: int, b: int) -> int:
"""
Add b to a.
Args:
a: first int number
b: second int number
"""
return a + b
@tool
def substract(a: int, b: int) -> int:
"""
Subtract b from a.
Args:
a: first int number
b: second int number
"""
return a - b
@tool
def multiply(a: int, b: int) -> int:
"""
Multiply a by b.
Args:
a: first int number
b: second int number
"""
return a * b
@tool
def divide(a: int, b: int) -> int:
"""
Divide a by b.
Args:
a: first int number
b: second int number
"""
if b == 0:
raise ValueError("Can't divide by zero.")
return a / b
@tool
def mod(a: int, b: int) -> int:
"""
Remainder of a devided by b.
Args:
a: first int number
b: second int number
"""
return a % b
@tool
def wiki_search(query: str) -> str:
"""
Search Wikipedia.
Args:
query: what to search for
"""
search_docs = WikipediaLoader(query=query, load_max_docs=3).load()
formatted_search_docs = "".join(
[
f'<START source="{doc.metadata["source"]}">{doc.page_content[:1000]}<END>'
for doc in search_docs
])
return {"wiki_results": formatted_search_docs}
@tool
def arvix_search(query: str) -> str:
"""
Search arXiv which is online archive of preprint and postprint manuscripts
for different fields of science.
Args:
query: what to search for
"""
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
formatted_search_docs = "".join(
[
f'<START source="{doc.metadata["source"]}">{doc.page_content[:1000]}<END>'
for doc in search_docs
])
return {"arvix_results": formatted_search_docs}
@tool
def web_search(query: str) -> str:
"""
Search WEB.
Args:
query: what to search for
"""
search_docs = TavilySearchResults(max_results=3, include_answer=True).invoke({"query": query})
formatted_search_docs = "".join(
[
f'<START source="{doc["url"]}">{doc["content"][:1000]}<END>'
for doc in search_docs
])
return {"web_results": formatted_search_docs}
@tool
def open_web_page(url: str) -> str:
"""
Open web page and get its content.
Args:
url: web page url in ""
"""
search_docs = TavilyExtract().invoke({"urls": [url]})
formatted_search_docs = f'<START source="{search_docs["results"][0]["url"]}">{search_docs["results"][0]["raw_content"][:1000]}<END>'
return {"web_page_content": formatted_search_docs}
@tool
def youtube_transcript(url: str) -> str:
"""
Get transcript of YouTube video.
Args:
url: YouTube video url in ""
"""
video_id = url.partition("https://www.youtube.com/watch?v=")[2]
transcript = YouTubeTranscriptApi.get_transcript(video_id)
transcript_text = " ".join([item["text"] for item in transcript])
return {"youtube_transcript": transcript_text}
tools = [
add,
substract,
multiply,
divide,
mod,
wiki_search,
arvix_search,
web_search,
open_web_page,
youtube_transcript,
]
# System prompt
system_prompt = f"""
You are a general AI assistant. I will ask you a question.
First, provide a step-by-step explanation of your reasoning to arrive at the answer.
Then, respond with your final answer in a single line, formatted as follows: "FINAL ANSWER: [YOUR FINAL ANSWER]".
[YOUR FINAL ANSWER] should be a number, a string, or a comma-separated list of numbers and/or strings, depending on the question.
If the answer is a number, do not use commas or units (e.g., $, %) unless specified.
If the answer is a string, do not use articles or abbreviations (e.g., for cities), and write digits in plain text unless specified.
If the answer is a comma-separated list, apply the above rules for each element based on whether it is a number or a string.
"""
system_message = SystemMessage(content=system_prompt)
# Build graph
def build_graph():
"""Build LangGrapth graph of agent."""
# Language model and tools
llm = ChatOpenAI(
model="gpt-4.1",
temperature=0,
max_retries=2
)
llm_with_tools = llm.bind_tools(tools, strict=True)
# Nodes
def assistant(state: MessagesState):
"""Assistant node."""
return {"messages": [llm_with_tools.invoke([system_message] + state["messages"])]}
# 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", tools_condition)
builder.add_edge("tools", "assistant")
# Compile graph
return builder.compile()
# Testing and solving particular tasks
if __name__ == "__main__":
agent = build_graph()
question = """
Review the chess position provided in the image. It is black's turn.
Provide the correct next move for black which guarantees a win.
Please provide your response in algebraic notation.
"""
content_urls = {
"image": "https://agents-course-unit4-scoring.hf.space/files/cca530fc-4052-43b2-b130-b30968d8aa44",
"audio": None
}
# Define user message and add all the content
content = [
{
"type": "text",
"text": question
}
]
if content_urls["image"]:
image_data = base64.b64encode(httpx.get(content_urls["image"]).content).decode("utf-8")
content.append(
{
"type": "image",
"source_type": "base64",
"data": image_data,
"mime_type": "image/jpeg"
}
)
if content_urls["audio"]:
audio_data = base64.b64encode(httpx.get(content_urls["audio"]).content).decode("utf-8")
content.append(
{
"type": "audio",
"source_type": "base64",
"data": audio_data,
"mime_type": "audio/wav"
}
)
messages = {
"role": "user",
"content": content
}
# Run agent on the question
messages = agent.invoke({"messages": messages})
for message in messages["messages"]:
message.pretty_print()
answer = messages["messages"][-1].content
index = answer.find("FINAL ANSWER: ")
print("\n")
print("="*30)
if index == -1:
print(answer)
print(answer[index+14:])
print("="*30)
|