hanshan1988 commited on
Commit
d63c54e
·
1 Parent(s): 54c0bd1

updated changeds

Browse files
Files changed (2) hide show
  1. app.py +5 -60
  2. tools.py +82 -0
app.py CHANGED
@@ -20,18 +20,16 @@ from langgraph.graph import StateGraph, START, END
20
  # from langchain_huggingface.llms import HuggingFaceEndpoint
21
  from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
22
  from langgraph.graph.message import add_messages
23
- from langchain_community.utilities import WikipediaAPIWrapper
24
- from langchain_community.tools import WikipediaQueryRun
25
- from langchain_community.document_loaders import YoutubeLoader, WebBaseLoader
26
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
27
  from langgraph.prebuilt import ToolNode, tools_condition
28
- from langchain.tools import tool
 
29
 
30
  # Initialize the Hugging Face model
31
 
32
  llm = HuggingFaceEndpoint(
33
- repo_id="Qwen/Qwen3-30B-A3B", # "Qwen/Qwen2.5-72B-Instruct",
34
- provider="nebius", # "hf-inference",
35
  max_new_tokens=8192,
36
  do_sample=False,
37
  # temperature=0.,
@@ -39,66 +37,13 @@ llm = HuggingFaceEndpoint(
39
 
40
  chat_model = ChatHuggingFace(llm=llm)
41
 
42
- # Define tools
43
-
44
-
45
- @tool
46
- def fetch_website(url:str) -> str:
47
- """Fetch the content of a website.
48
- Args:
49
- url: The URL of the website to fetch.
50
- Returns:
51
- The title and content of the website.
52
- """
53
- loader = WebBaseLoader(url)
54
- docs = loader.load()
55
- return docs[0].page_content
56
-
57
- @tool
58
- def ask_wiki(query: str) -> str:
59
- """Retrieve information from Wikipedia based on a user query.
60
- Args:
61
- query: A user query.
62
- Returns:
63
- A single string containing the retrieved article from Wikipedia.
64
- """
65
- if not query.strip():
66
- return "Please provide a valid query."
67
- try:
68
- wiki_toolapi_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=8000)
69
- wiki_tool = WikipediaQueryRun(api_wrapper=wiki_toolapi_wrapper)
70
- result = wiki_tool.run(query)
71
- return result
72
- except Exception as e:
73
- return f"Error retrieving information: {str(e)}"
74
-
75
- @tool
76
- def youtube_transcript(url: str) -> str:
77
- """Retrieve transcript from Youtube based url.
78
- Args:
79
- url: input youtube url.
80
- Returns:
81
- A single string containing the transcript of the youtube videos.
82
- """
83
- max_attempts = 5 # Set a maximum number of attempts
84
- attempts = 0
85
- loader = YoutubeLoader.from_youtube_url(url, add_video_info=True)
86
- while attempts < max_attempts:
87
- try:
88
- docs = loader.load()
89
- return docs[0].page_content
90
- except Exception as e:
91
- attempts += 1
92
- print(f"Attempt {attempts} failed: {e}")
93
- # Optionally add a delay before retrying
94
- time.sleep(1) # Import the time module
95
- return "Failed to retrieve transcript after multiple attempts."
96
 
97
  # Equip llm with tools
98
  tools_list = [
99
  fetch_website,
100
  ask_wiki,
101
  youtube_transcript,
 
102
  ]
103
 
104
  llm_with_tools = chat_model.bind_tools(
 
20
  # from langchain_huggingface.llms import HuggingFaceEndpoint
21
  from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
22
  from langgraph.graph.message import add_messages
 
 
 
23
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
24
  from langgraph.prebuilt import ToolNode, tools_condition
25
+
26
+ from tools import fetch_website, ask_wiki, youtube_transcript, python_repl_tool
27
 
28
  # Initialize the Hugging Face model
29
 
30
  llm = HuggingFaceEndpoint(
31
+ repo_id="XiaomiMiMo/MiMo-V2-Flash", # "Qwen/Qwen2.5-72B-Instruct",
32
+ provider="novita", # "hf-inference",
33
  max_new_tokens=8192,
34
  do_sample=False,
35
  # temperature=0.,
 
37
 
38
  chat_model = ChatHuggingFace(llm=llm)
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Equip llm with tools
42
  tools_list = [
43
  fetch_website,
44
  ask_wiki,
45
  youtube_transcript,
46
+ python_repl_tool
47
  ]
48
 
49
  llm_with_tools = chat_model.bind_tools(
tools.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from langchain.tools import tool
3
+ from langchain_community.utilities import WikipediaAPIWrapper
4
+ from langchain_community.tools import WikipediaQueryRun
5
+ from langchain_community.document_loaders import YoutubeLoader, WebBaseLoader
6
+ from langchain_experimental.utilities import PythonREPL
7
+
8
+ # Initialize Python REPL
9
+ python_repl = PythonREPL()
10
+
11
+ @tool
12
+ def fetch_website(url:str) -> str:
13
+ """Fetch the content of a website.
14
+ Args:
15
+ url: The URL of the website to fetch.
16
+ Returns:
17
+ The title and content of the website.
18
+ """
19
+ loader = WebBaseLoader(url)
20
+ docs = loader.load()
21
+ return docs[0].page_content
22
+
23
+ @tool
24
+ def ask_wiki(query: str) -> str:
25
+ """Retrieve information from Wikipedia based on a user query.
26
+ Args:
27
+ query: A user query.
28
+ Returns:
29
+ A single string containing the retrieved article from Wikipedia.
30
+ """
31
+ if not query.strip():
32
+ return "Please provide a valid query."
33
+ try:
34
+ wiki_toolapi_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=8000)
35
+ wiki_tool = WikipediaQueryRun(api_wrapper=wiki_toolapi_wrapper)
36
+ result = wiki_tool.run(query)
37
+ return result
38
+ except Exception as e:
39
+ return f"Error retrieving information: {str(e)}"
40
+
41
+ @tool
42
+ def youtube_transcript(url: str) -> str:
43
+ """Retrieve transcript from Youtube based url.
44
+ Args:
45
+ url: input youtube url.
46
+ Returns:
47
+ A single string containing the transcript of the youtube videos.
48
+ """
49
+ max_attempts = 5 # Set a maximum number of attempts
50
+ attempts = 0
51
+ loader = YoutubeLoader.from_youtube_url(url, add_video_info=True)
52
+ while attempts < max_attempts:
53
+ try:
54
+ docs = loader.load()
55
+ return docs[0].page_content
56
+ except Exception as e:
57
+ attempts += 1
58
+ print(f"Attempt {attempts} failed: {e}")
59
+ # Optionally add a delay before retrying
60
+ time.sleep(1) # Import the time module
61
+ return "Failed to retrieve transcript after multiple attempts."
62
+
63
+ @tool
64
+ def python_repl_tool(code: str) -> str:
65
+ """
66
+ Execute Python code and return the output.
67
+
68
+ Use this tool to run Python code for calculations, data analysis,
69
+ or any computational tasks. The code runs in a persistent Python
70
+ environment, so variables and imports are preserved between calls.
71
+
72
+ Args:
73
+ code: Python code to execute
74
+
75
+ Returns:
76
+ The output of the code execution (stdout) or error message
77
+ """
78
+ try:
79
+ result = python_repl.run(code)
80
+ return result if result else "Code executed successfully (no output)"
81
+ except Exception as e:
82
+ return f"Error: {str(e)}"