Samuel Thomas commited on
Commit
7f930e4
·
1 Parent(s): bb2d086
Files changed (3) hide show
  1. .gitignore +2 -1
  2. app.py +28 -68
  3. mytools.py +48 -0
.gitignore CHANGED
@@ -1 +1,2 @@
1
- __pycache__/
 
 
1
+ __pycache__/
2
+ .env
app.py CHANGED
@@ -1,27 +1,42 @@
1
  import gradio as gr
2
  from typing import TypedDict, Annotated
3
  from huggingface_hub import InferenceClient, login
4
- import random
5
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFacePipeline
6
  #from langchain.schema import AIMessage, HumanMessage
7
  from langgraph.graph.message import add_messages
 
8
  from langgraph.prebuilt import ToolNode, tools_condition
9
  from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
10
- from langchain.tools import Tool
11
- import os
12
  import datasets
13
- from langchain.docstore.document import Document
14
  from langgraph.graph import START, StateGraph
15
- from langchain_community.retrievers import BM25Retriever
16
- from langchain_community.tools import DuckDuckGoSearchRun
 
17
 
 
18
 
19
  HUGGINGFACEHUB_API_TOKEN = os.environ["HUGGINGFACEHUB_API_TOKEN"]
20
  login(token=HUGGINGFACEHUB_API_TOKEN, add_to_git_credential=True)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Load the dataset
23
  guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
24
 
 
25
  # Convert dataset entries into Document objects
26
  docs = [
27
  Document(
@@ -36,9 +51,9 @@ docs = [
36
  for guest in guest_dataset
37
  ]
38
 
39
-
40
  bm25_retriever = BM25Retriever.from_documents(docs)
41
 
 
42
  def extract_text(query: str) -> str:
43
  """Retrieves detailed information about gala guests based on their name or relation."""
44
  results = bm25_retriever.invoke(query)
@@ -47,18 +62,12 @@ def extract_text(query: str) -> str:
47
  else:
48
  return "No matching guest information found."
49
 
50
- llm = HuggingFaceEndpoint(
51
- #repo_id="HuggingFaceH4/zephyr-7b-beta",
52
- repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
53
- task="text-generation",
54
- max_new_tokens=512,
55
- do_sample=False,
56
- repetition_penalty=1.03,
57
- timeout=240,
58
  )
59
 
60
- model = ChatHuggingFace(llm=llm, verbose=True)
61
-
62
  def predict(message, history):
63
  # Convert Gradio history to LangChain message format
64
  history_langchain_format = []
@@ -82,57 +91,8 @@ def predict(message, history):
82
 
83
  # setup agents
84
 
85
- guest_info_tool = Tool(
86
- name="guest_info_retriever",
87
- func=extract_text,
88
- description="Retrieves detailed information about gala guests based on their name or relation."
89
- )
90
- """
91
- search_tool = DuckDuckGoSearchRun()
92
-
93
- def get_weather_info(location: str) -> str:
94
- Fetches dummy weather information for a given location.
95
- # Dummy weather data
96
- weather_conditions = [
97
- {"condition": "Rainy", "temp_c": 15},
98
- {"condition": "Clear", "temp_c": 25},
99
- {"condition": "Windy", "temp_c": 20}
100
- ]
101
- # Randomly select a weather condition
102
- data = random.choice(weather_conditions)
103
- return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
104
-
105
- # Initialize the tool
106
- weather_info_tool = Tool(
107
- name="get_weather_info",
108
- func=get_weather_info,
109
- description="Fetches dummy weather information for a given location."
110
- )
111
-
112
-
113
- def get_hub_stats(author: str) -> str:
114
- Fetches the most downloaded model from a specific author on the Hugging Face Hub.
115
- try:
116
- # List models from the specified author, sorted by downloads
117
- models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
118
-
119
- if models:
120
- model = models[0]
121
- return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
122
- else:
123
- return f"No models found for author {author}."
124
- except Exception as e:
125
- return f"Error fetching models for {author}: {str(e)}"
126
-
127
- # Initialize the tool
128
- hub_stats_tool = Tool(
129
- name="get_hub_stats",
130
- func=get_hub_stats,
131
- description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."
132
- )
133
- """
134
- #tools = [guest_info_tool, search_tool, weather_info_tool, hub_stats_tool]
135
- tools = [guest_info_tool]
136
  chat_with_tools = model.bind_tools(tools)
137
 
138
  # Generate the AgentState and Agent graph
 
1
  import gradio as gr
2
  from typing import TypedDict, Annotated
3
  from huggingface_hub import InferenceClient, login
 
4
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFacePipeline
5
  #from langchain.schema import AIMessage, HumanMessage
6
  from langgraph.graph.message import add_messages
7
+ from langchain.docstore.document import Document
8
  from langgraph.prebuilt import ToolNode, tools_condition
9
  from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
10
+ from langchain_community.retrievers import BM25Retriever
 
11
  import datasets
12
+ import os
13
  from langgraph.graph import START, StateGraph
14
+ from langchain.tools import Tool
15
+ from mytools import search_tool, weather_info_tool, hub_stats_tool
16
+ from dotenv import load_dotenv
17
 
18
+ load_dotenv()
19
 
20
  HUGGINGFACEHUB_API_TOKEN = os.environ["HUGGINGFACEHUB_API_TOKEN"]
21
  login(token=HUGGINGFACEHUB_API_TOKEN, add_to_git_credential=True)
22
 
23
+ llm = HuggingFaceEndpoint(
24
+ #repo_id="HuggingFaceH4/zephyr-7b-beta",
25
+ repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
26
+ task="text-generation",
27
+ max_new_tokens=512,
28
+ do_sample=False,
29
+ repetition_penalty=1.03,
30
+ timeout=240,
31
+ )
32
+
33
+ model = ChatHuggingFace(llm=llm, verbose=True)
34
+
35
+
36
  # Load the dataset
37
  guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
38
 
39
+
40
  # Convert dataset entries into Document objects
41
  docs = [
42
  Document(
 
51
  for guest in guest_dataset
52
  ]
53
 
 
54
  bm25_retriever = BM25Retriever.from_documents(docs)
55
 
56
+
57
  def extract_text(query: str) -> str:
58
  """Retrieves detailed information about gala guests based on their name or relation."""
59
  results = bm25_retriever.invoke(query)
 
62
  else:
63
  return "No matching guest information found."
64
 
65
+ guest_info_tool = Tool(
66
+ name="guest_info_retriever",
67
+ func=extract_text,
68
+ description="Retrieves detailed information about gala guests based on their name or relation."
 
 
 
 
69
  )
70
 
 
 
71
  def predict(message, history):
72
  # Convert Gradio history to LangChain message format
73
  history_langchain_format = []
 
91
 
92
  # setup agents
93
 
94
+ tools = [guest_info_tool, search_tool, weather_info_tool, hub_stats_tool]
95
+ #tools = [guest_info_tool]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  chat_with_tools = model.bind_tools(tools)
97
 
98
  # Generate the AgentState and Agent graph
mytools.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.tools import Tool
2
+ from langchain_community.tools import DuckDuckGoSearchRun
3
+ import random
4
+ from huggingface_hub import list_models
5
+
6
+
7
+ search_tool = DuckDuckGoSearchRun()
8
+
9
+ def get_weather_info(location: str) -> str:
10
+ """Fetches dummy weather information for a given location."""
11
+ # Dummy weather data
12
+ weather_conditions = [
13
+ {"condition": "Rainy", "temp_c": 15},
14
+ {"condition": "Clear", "temp_c": 25},
15
+ {"condition": "Windy", "temp_c": 20}
16
+ ]
17
+ # Randomly select a weather condition
18
+ data = random.choice(weather_conditions)
19
+ return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
20
+
21
+ # Initialize the tool
22
+ weather_info_tool = Tool(
23
+ name="get_weather_info",
24
+ func=get_weather_info,
25
+ description="Fetches dummy weather information for a given location."
26
+ )
27
+
28
+
29
+ def get_hub_stats(author: str) -> str:
30
+ """Fetches the most downloaded model from a specific author on the Hugging Face Hub."""
31
+ try:
32
+ # List models from the specified author, sorted by downloads
33
+ models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
34
+
35
+ if models:
36
+ model = models[0]
37
+ return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
38
+ else:
39
+ return f"No models found for author {author}."
40
+ except Exception as e:
41
+ return f"Error fetching models for {author}: {str(e)}"
42
+
43
+ # Initialize the tool
44
+ hub_stats_tool = Tool(
45
+ name="get_hub_stats",
46
+ func=get_hub_stats,
47
+ description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."
48
+ )