Andrei Sferdian commited on
Commit ·
692e5e8
1
Parent(s): 90f4ddb
Add tool to fetch raw wiki pages
Browse filesFiles changed (10)
12.venv.env1234.venv.env__pycache__
5 unmodified lines67891011121314151617181920215 unmodified lines# Run all teststest:pytest# Run tests from a specific moduletest-module:pytest $(module)# Run a specific test from a moduletest-case:pytest $(module)::$(test)# Run tests matching a regex patterntest-pattern:pytest -k $(pattern)5 unmodified lines67891011121314151617181920215 unmodified lines# Run all teststest:pytest -v# Run tests from a specific moduletest-module:pytest -v $(module)# Run a specific test from a moduletest-case:pytest -v $(module)::$(test)# Run tests matching a regex patterntest-pattern:pytest -v -k $(pattern)
10 unmodified lines111213141516171819202122232410 unmodified linesfrom langgraph.prebuilt import create_react_agentfrom langgraph.prebuilt.chat_agent_executor import AgentStatefrom tools import GetYouTubeTranscriptmodel = ChatTogether(model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",temperature=0,max_tokens=2000,max_retries=3,).bind_tools([DuckDuckGoSearchRun(), GetYouTubeTranscript()])agent = create_react_agent(model=model,10 unmodified lines111213141516171819202122232410 unmodified linesfrom langgraph.prebuilt import create_react_agentfrom langgraph.prebuilt.chat_agent_executor import AgentStatefrom tools import GetYouTubeTranscript, get_wikipedia_pagemodel = ChatTogether(model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",temperature=0,max_tokens=2000,max_retries=3,).bind_tools([DuckDuckGoSearchRun(), GetYouTubeTranscript(), get_wikipedia_page])agent = create_react_agent(model=model,
54 unmodified lines55565758596058 unmodified lines11912012112212312413 unmodified lines13813914014114214326 unmodified lines17017117217317417513 unmodified lines18919019119219319454 unmodified lineshuggingface-hub==0.30.2humanfriendly==10.0idna==3.10Jinja2==3.1.6jiter==0.9.0joblib==1.4.258 unmodified linespi_heif==0.22.0pikepdf==9.7.0pillow==11.2.1primp==0.15.0propcache==0.3.1proto-plus==1.26.113 unmodified linespypdf==5.4.0pypdfium2==4.30.1pytesseract==0.3.13python-dateutil==2.9.0.post0python-docx==1.1.2python-dotenv==1.1.026 unmodified linestiktoken==0.9.0timm==1.0.15tokenizers==0.21.1tomlkit==0.13.2torch==2.7.0torchvision==0.22.013 unmodified linesuvicorn==0.34.2webencodings==0.5.1websockets==15.0.1wrapt==1.17.2xlrd==2.0.1XlsxWriter==3.2.354 unmodified lines5556575859606158 unmodified lines12012112212312412512613 unmodified lines14014114214314414514626 unmodified lines17317417517617717817913 unmodified lines19319419519619719819954 unmodified lineshuggingface-hub==0.30.2humanfriendly==10.0idna==3.10iniconfig==2.1.0Jinja2==3.1.6jiter==0.9.0joblib==1.4.258 unmodified linespi_heif==0.22.0pikepdf==9.7.0pillow==11.2.1pluggy==1.5.0primp==0.15.0propcache==0.3.1proto-plus==1.26.113 unmodified linespypdf==5.4.0pypdfium2==4.30.1pytesseract==0.3.13pytest==8.3.5python-dateutil==2.9.0.post0python-docx==1.1.2python-dotenv==1.1.026 unmodified linestiktoken==0.9.0timm==1.0.15tokenizers==0.21.1tomli==2.2.1tomlkit==0.13.2torch==2.7.0torchvision==0.22.013 unmodified linesuvicorn==0.34.2webencodings==0.5.1websockets==15.0.1wikipedia==1.4.0wrapt==1.17.2xlrd==2.0.1XlsxWriter==3.2.3
1234567891011121314151617181920212223242526272829import pytestfrom tools import GetYouTubeTranscript@pytest.fixturedef tool():return GetYouTubeTranscript()def test_valid_youtube_url(tool):# Use a real YouTube video URL with a publicly available transcripturl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley" # Replace with a valid video IDresult = tool._run(url)assert ("Never gonna give you up" in result) # Check for a known part of the transcriptdef test_invalid_youtube_url(tool):url = "https://www.youtube.com/watch?v="result = tool._run(url)assert result == "Video ID not found in the URL"def test_non_youtube_url(tool):url = "https://www.example.com"result = tool._run(url)assert result == "Video ID not found in the URL"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import tempfileimport pytestfrom langchain_community.tools import DuckDuckGoSearchRunfrom tools import GetYouTubeTranscript, get_wikipedia_page@pytest.fixturedef yt():return GetYouTubeTranscript()@pytest.fixturedef ddg():return DuckDuckGoSearchRun()def test_valid_youtube_url(yt):# Use a real YouTube video URL with a publicly available transcripturl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley" # Replace with a valid video IDresult = yt._run(url)assert ("Never gonna give you up" in result) # Check for a known part of the transcriptdef test_invalid_youtube_url(yt):url = "https://www.youtube.com/watch?v="result = yt._run(url)assert result == "Video ID not found in the URL"def test_non_youtube_url(yt):url = "https://www.example.com"result = yt._run(url)assert result == "Video ID not found in the URL"def test_ddg_search(ddg: DuckDuckGoSearchRun):query = "Mercedes Sosa live albums"result = ddg._run(query)print(result)assert isinstance(result, str) # Check if the result is a stringdef test_wiki_get_content():query = "Mercedes Sosa"content = get_wikipedia_page(query)temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w", encoding="utf-8", dir="/tmp")with open(temp_file.name, "w", encoding="utf-8") as file:file.write(content)assert content != ""assert "Error" not in content
1234567from .get_wikipedia_page import get_wikipedia_pagefrom .get_youtube_transcript import GetYouTubeTranscript
__all__ = [ "get_wikipedia_page", "GetYouTubeTranscript",]12345678910111213141516171819202122232425262728293031import requestsimport wikipedia as wikifrom langchain_core.tools import tool
@tool(description="Get Wikipedia page content")def get_wikipedia_page(query: str) -> str: """ Fetches the content of a Wikipedia page based on the given query. This function searches for Wikipedia pages matching the query, retrieves the first result, and fetches its content. If no pages are found or an error occurs during the process, an appropriate message or error description is returned. Args: query (str): The search query to look for on Wikipedia. Returns: str: The content of the Wikipedia page if found and successfully fetched, or an error message if no page is found or an exception occurs. """
try: titles = wiki.search(query) if not titles or len(titles) == 0: return "No Wikipedia page found for the given query."
page = wiki.page(titles[0]) url = page.url response = requests.get(url) response.raise_for_status() return response.text except Exception as e: return f"Error fetching Wikipedia page: {e}".gitignore+2
Makefile+4-4
__pycache__/test_tools.cpython-310-pytest-8.3.5.pycDeleted
__pycache__/tools.cpython-310.pycDeleted
agent.py+2-2
requirements.txt+5
test_tools.py+35-8
tools/__init__.pyAdded+7
tools/get_wikipedia_page.pyAdded+31
tools.py → tools/get_youtube_transcript.pyRenamed