Nguyen Nhu Trung commited on
Commit
69b065f
·
1 Parent(s): 8e8b0af
Files changed (2) hide show
  1. agent.py +57 -2
  2. requirements.txt +3 -1
agent.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
  from dotenv import load_dotenv
 
 
3
  from langgraph.graph import START, StateGraph, MessagesState
4
  from langgraph.prebuilt import tools_condition
5
  from langgraph.prebuilt import ToolNode
@@ -9,9 +11,58 @@ from langchain_community.document_loaders import WikipediaLoader
9
  from langchain_core.messages import SystemMessage, HumanMessage
10
  from langchain_core.tools import tool
11
  from langchain_core.output_parsers import StrOutputParser
 
 
 
12
 
13
  load_dotenv()
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @tool
16
  def wiki_search(query: str) -> str:
17
  """Search Wikipedia for a query and return maximum 2 results.
@@ -28,12 +79,13 @@ def wiki_search(query: str) -> str:
28
 
29
  @tool
30
  def web_search(query: str) -> str:
31
- """Search DuckDuckGo for a query and return maximum 3 results.
32
 
33
  Args:
34
  query: The search query."""
35
- search_docs = DuckDuckGoSearchResults(max_results=3).invoke(query)
36
  return {"web_results": search_docs}
 
37
 
38
  system_prompt = "You are a helpful assistant"
39
  sys_msg = SystemMessage(content=system_prompt)
@@ -41,6 +93,9 @@ sys_msg = SystemMessage(content=system_prompt)
41
  tools = [
42
  wiki_search,
43
  web_search,
 
 
 
44
  ]
45
 
46
  llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
 
1
  import os
2
  from dotenv import load_dotenv
3
+ import pandas as pd
4
+ import json
5
  from langgraph.graph import START, StateGraph, MessagesState
6
  from langgraph.prebuilt import tools_condition
7
  from langgraph.prebuilt import ToolNode
 
11
  from langchain_core.messages import SystemMessage, HumanMessage
12
  from langchain_core.tools import tool
13
  from langchain_core.output_parsers import StrOutputParser
14
+ from langchain_core.tools import Tool
15
+ from langchain_experimental.utilities import PythonREPL
16
+ import assemblyai as aai
17
 
18
  load_dotenv()
19
 
20
+ aai.settings.api_key = os.getenv("ASSEMBLY_AI_KEY")
21
+
22
+ repl_tool = Tool(
23
+ name="python_repl",
24
+ description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
25
+ func=PythonREPL().run,
26
+ )
27
+
28
+ @tool
29
+ def read_excel_file(excel_file_path: str) -> str:
30
+ """Read the content of excel file.
31
+
32
+ Args:
33
+ excel_file_path: path to excel file
34
+ """
35
+ # Load the Excel file using pandas
36
+ try:
37
+ # Read the Excel file
38
+ df = pd.read_excel(excel_file_path, sheet_name=None) # sheet_name=None loads all sheets
39
+
40
+ # Convert each sheet to a dictionary of rows
41
+ json_output = {}
42
+ for sheet_name, sheet_data in df.items():
43
+ # Convert the dataframe to a list of dictionaries (rows)
44
+ json_output[sheet_name] = sheet_data.to_dict(orient="records")
45
+
46
+ # Convert the result to a JSON formatted string
47
+ json_result = json.dumps(json_output, indent=4)
48
+ return json_result
49
+ except Exception as e:
50
+ return str(e)
51
+
52
+
53
+ @tool
54
+ def transcribe_audio(file_path: str) -> str:
55
+ """Transcribe the audio file into text.
56
+
57
+ Args:
58
+ file_path: path to audio file
59
+ """
60
+ config = aai.TranscriptionConfig(speech_model=aai.SpeechModel.best)
61
+ transcript = aai.Transcriber(config=config).transcribe(file_path)
62
+ if transcript.status == "error":
63
+ raise RuntimeError(f"Transcription failed: {transcript.error}")
64
+ return f"Here is the transcript: {transcript.text}"
65
+
66
  @tool
67
  def wiki_search(query: str) -> str:
68
  """Search Wikipedia for a query and return maximum 2 results.
 
79
 
80
  @tool
81
  def web_search(query: str) -> str:
82
+ """Search DuckDuckGo for a query and return maximum 5 results.
83
 
84
  Args:
85
  query: The search query."""
86
+ search_docs = DuckDuckGoSearchResults(max_results=5).invoke(query)
87
  return {"web_results": search_docs}
88
+
89
 
90
  system_prompt = "You are a helpful assistant"
91
  sys_msg = SystemMessage(content=system_prompt)
 
93
  tools = [
94
  wiki_search,
95
  web_search,
96
+ repl_tool,
97
+ read_excel_file,
98
+ transcribe_audio
99
  ]
100
 
101
  llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
requirements.txt CHANGED
@@ -5,4 +5,6 @@ dotenv
5
  langchain-groq
6
  langchain-community
7
  wikipedia
8
- duckduckgo-search
 
 
 
5
  langchain-groq
6
  langchain-community
7
  wikipedia
8
+ duckduckgo-search
9
+ langchain-experimental
10
+ assemblyai