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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.messages import HumanMessage
|
| 2 |
+
from langchain_core.tools import tool, Tool
|
| 3 |
+
from langchain_together import ChatTogether
|
| 4 |
+
from langgraph.prebuilt import create_react_agent
|
| 5 |
+
from langchain_community.retrievers import WikipediaRetriever
|
| 6 |
+
from langchain_community.tools import BraveSearch
|
| 7 |
+
from langchain_experimental.utilities import PythonREPL
|
| 8 |
+
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 9 |
+
import requests
|
| 10 |
+
from langgraph_supervisor import create_supervisor
|
| 11 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 12 |
+
from pytubefix import extract, YouTube
|
| 13 |
+
import whisper
|
| 14 |
+
from qwen_vl_utils import process_vision_info
|
| 15 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
| 16 |
+
|
| 17 |
+
@tool
|
| 18 |
+
def wiki_search(query: str) -> str:
|
| 19 |
+
"""Search Wikipedia for query and return maximum 3 results
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
query (str): query to search on Wikipedia
|
| 23 |
+
Returns:
|
| 24 |
+
wiki_result (str): result of search
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
retriever = WikipediaRetriever()
|
| 28 |
+
wiki_result = retriever.invoke(query)
|
| 29 |
+
return wiki_result
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"wiki_search failed {e}"
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def query_image(query: str, image_url: str):
|
| 35 |
+
"""Analyze the query on an image using a VLM
|
| 36 |
+
|
| 37 |
+
Args:
|
| 38 |
+
query (str): query about the image
|
| 39 |
+
image_url (str): link to the image
|
| 40 |
+
Returns:
|
| 41 |
+
response (str): response to the query on image
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 45 |
+
"Qwen/Qwen2.5-VL-3B-Instruct", torch_dtype="auto", device_map="auto"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct")
|
| 49 |
+
|
| 50 |
+
messages = [
|
| 51 |
+
{
|
| 52 |
+
"role": "user",
|
| 53 |
+
"content": [
|
| 54 |
+
{
|
| 55 |
+
"type": "image",
|
| 56 |
+
"image": image_url,
|
| 57 |
+
"max_pixels": 360 * 420,
|
| 58 |
+
},
|
| 59 |
+
{"type": "text", "text": query},
|
| 60 |
+
],
|
| 61 |
+
}
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
text = processor.apply_chat_template(
|
| 65 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 66 |
+
)
|
| 67 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 68 |
+
inputs = processor(
|
| 69 |
+
text=[text],
|
| 70 |
+
images=image_inputs,
|
| 71 |
+
videos=video_inputs,
|
| 72 |
+
padding=True,
|
| 73 |
+
return_tensors="pt",
|
| 74 |
+
)
|
| 75 |
+
inputs = inputs.to("cuda")
|
| 76 |
+
|
| 77 |
+
# Inference
|
| 78 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
| 79 |
+
generated_ids_trimmed = [
|
| 80 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 81 |
+
]
|
| 82 |
+
output_text = processor.batch_decode(
|
| 83 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 84 |
+
)
|
| 85 |
+
print(output_text)
|
| 86 |
+
return output_text
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"query_image failed {e}"
|
| 90 |
+
|
| 91 |
+
@tool
|
| 92 |
+
def reverse_string(input_string: str) -> str:
|
| 93 |
+
"""Reverse the character order of input string.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
input_string (str): string to reverse
|
| 97 |
+
Returns:
|
| 98 |
+
reversed_string (str): reversed string
|
| 99 |
+
"""
|
| 100 |
+
try:
|
| 101 |
+
reversed_string = input_string[::-1]
|
| 102 |
+
reversed_string = f"The reversed string returned from reverse_string function is: {reversed_string}"
|
| 103 |
+
return reversed_string
|
| 104 |
+
except Exception as e:
|
| 105 |
+
return f"reverse_string failed {e}"
|
| 106 |
+
|
| 107 |
+
repl = PythonREPL()
|
| 108 |
+
python_repl_tool = Tool(
|
| 109 |
+
name="python_repl",
|
| 110 |
+
description="""A Python shell. Use this to execute python commands.
|
| 111 |
+
Input should be a valid python command.
|
| 112 |
+
Input should be a valid Python expression or script.
|
| 113 |
+
If you want to see the output of a value, you should print it out with `print(...)`.
|
| 114 |
+
Always return the printed code output.
|
| 115 |
+
Example: print(2 + 2) → will return 4
|
| 116 |
+
Do NOT execute code that could be harmful to the host system.
|
| 117 |
+
You are allowed to download files from URLs.""",
|
| 118 |
+
func=repl.run
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class langgraph_agent:
|
| 123 |
+
def __init__(self):
|
| 124 |
+
llm = ChatTogether(
|
| 125 |
+
model="Qwen/Qwen3-235B-A22B-fp8-tput",
|
| 126 |
+
temperature=0
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
helper_llm = ChatTogether(
|
| 130 |
+
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 131 |
+
temperature=0
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
research_agent = create_react_agent(
|
| 135 |
+
llm,
|
| 136 |
+
tools=research_tools,
|
| 137 |
+
name="research_agent",
|
| 138 |
+
prompt=(
|
| 139 |
+
"You are a research agent. You have access to web_search tool to search the web, wiki_search tool to search wikipedia\n\n"
|
| 140 |
+
"INSTRUCTIONS:\n"
|
| 141 |
+
"- Assist ONLY with research tasks\n"
|
| 142 |
+
"- After you're done with your tasks, respond to the supervisor directly\n"
|
| 143 |
+
"- Respond ONLY with the results of your work, do NOT include ANY other text."
|
| 144 |
+
),
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
vision_agent = create_react_agent(
|
| 148 |
+
helper_llm,
|
| 149 |
+
tools=vision_tools,
|
| 150 |
+
name="vision_agent",
|
| 151 |
+
prompt=(
|
| 152 |
+
"You are a vision agent. You have access to the following tools: \n"
|
| 153 |
+
" query_image(query: str, image_url: str): \n"
|
| 154 |
+
" Args:\n"
|
| 155 |
+
" query (str): query on the image \n"
|
| 156 |
+
" image_url (str): link to the image \n"
|
| 157 |
+
" Returns:\n"
|
| 158 |
+
" response (str): response to the query after analyzing image \n\n"
|
| 159 |
+
" query_video(query: str, video_url: str): \n"
|
| 160 |
+
" Args:\n"
|
| 161 |
+
" query (str): query on the video\n"
|
| 162 |
+
" video_url (str): link to the video \n"
|
| 163 |
+
" Returns: \n"
|
| 164 |
+
" response (str): response to the query after analyzing video \n\n"
|
| 165 |
+
"INSTRUCTIONS:\n"
|
| 166 |
+
"- Assist ONLY with vision related tasks\n"
|
| 167 |
+
"- After you're done with your tasks, respond to the supervisor directly\n"
|
| 168 |
+
"- Respond ONLY with the results of your work, do NOT include ANY other text."
|
| 169 |
+
),
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
python_agent = create_react_agent(
|
| 173 |
+
helper_llm,
|
| 174 |
+
tools=[python_repl_tool],
|
| 175 |
+
name="python_agent",
|
| 176 |
+
prompt=(
|
| 177 |
+
"You are a python coding agent with access to a python REPL. You will be given a query and a link to a piece of python code. Retrieve and execute the linked code with python_repl tool to answer the query. \n\n"
|
| 178 |
+
"INSTRUCTIONS:\n"
|
| 179 |
+
"- Assist ONLY with python coding tasks\n"
|
| 180 |
+
"- You are allowed to download files from given URLs \n"
|
| 181 |
+
"- Do not execute code that can be harmful to host system \n"
|
| 182 |
+
"- If there is Exception thrown during execution, try to debug your code, then execute again. \n"
|
| 183 |
+
"- Always transfer any printed output from executed code to supervisor \n"
|
| 184 |
+
"- After you're done with your tasks, respond to the supervisor directly\n"
|
| 185 |
+
"- Respond ONLY with the results of your work, do NOT include ANY other text."
|
| 186 |
+
),
|
| 187 |
+
)
|