malimasood47 commited on
Commit
01babb7
·
verified ·
1 Parent(s): 133f029

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -15
app.py CHANGED
@@ -1,20 +1,69 @@
1
  # LangChain version is pinned to 0.3.0 in requirements.txt.
2
- # We must use the 'initialize_agent' pattern and not the 'create_react_agent' pattern (which was introduced later).
3
 
4
- # Correct Imports for LangChain v0.3.0:
5
  from langchain.agents import AgentExecutor
6
  from langchain.agents import initialize_agent, AgentType
 
 
 
 
7
 
8
- print("AgentExecutor and initialize_agent imported successfully for v0.3.0!")
9
-
10
- # NOTE: The old 'initialize_agent' method is the correct way to build agents
11
- # in the 0.3.0 version.
12
- # Example usage (assuming you define tools and an LLM):
13
- # def create_my_agent(llm, tools):
14
- # agent = initialize_agent(
15
- # tools,
16
- # llm,
17
- # agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
18
- # verbose=True
19
- # )
20
- # return agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # LangChain version is pinned to 0.3.0 in requirements.txt.
2
+ # We must use the 'initialize_agent' pattern.
3
 
4
+ # CORE IMPORTS
5
  from langchain.agents import AgentExecutor
6
  from langchain.agents import initialize_agent, AgentType
7
+ from langchain.llms import GooglePalm # Using a generic LLM wrapper for LangChain < 0.4.0
8
+ from langchain.tools import Tool
9
+ from typing import Optional, Type
10
+ from pydantic import BaseModel, Field
11
 
12
+ # --- TOOL DEFINITION ---
13
+ # 1. Define the input schema for the tool (Pydantic is required for structured input)
14
+ class CryptoPriceInput(BaseModel):
15
+ """Input for the Crypto Price Checker tool."""
16
+ cryptocurrency: str = Field(description="The name of the cryptocurrency (e.g., 'bitcoin', 'ethereum').")
17
+
18
+ # 2. Define the tool function
19
+ def get_crypto_price(cryptocurrency: str) -> str:
20
+ """
21
+ Returns the current price of a specified cryptocurrency.
22
+ Note: This is a placeholder function and returns a mock price.
23
+ """
24
+ crypto_prices = {
25
+ "bitcoin": "101,664.00",
26
+ "ethereum": "5,500.00",
27
+ "solana": "250.00",
28
+ }
29
+
30
+ price = crypto_prices.get(cryptocurrency.lower())
31
+ if price:
32
+ return f"💰 {cryptocurrency.capitalize()} current price is ${price}"
33
+ else:
34
+ return f"Could not find the current price for {cryptocurrency}. Please try 'bitcoin', 'ethereum', or 'solana'."
35
+
36
+ # 3. Create the LangChain Tool instance
37
+ crypto_price_tool = Tool(
38
+ name="Crypto Price Checker",
39
+ func=get_crypto_price,
40
+ description="Useful for finding the current price of major cryptocurrencies like bitcoin or ethereum.",
41
+ args_schema=CryptoPriceInput # Attach Pydantic schema
42
+ )
43
+
44
+ # --- AGENT SETUP ---
45
+ # 4. Initialize the LLM (Replace "YOUR_API_KEY" with an actual key or use environment variable)
46
+ # NOTE: In v0.3.0, GooglePalm was often used for Google models.
47
+ # If you intend to use the newer Gemini models, we would need to switch to a later version or use the specific community package import path.
48
+ # For simplicity, we are using the generic placeholder LLM here.
49
+ llm = GooglePalm(google_api_key="YOUR_API_KEY") # Replace with your actual key or load from env
50
+
51
+ # 5. Define the list of tools the agent can use
52
+ tools = [crypto_price_tool]
53
+
54
+ # 6. Initialize the Agent Executor
55
+ agent = initialize_agent(
56
+ tools,
57
+ llm,
58
+ agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
59
+ verbose=True # Set to True to see the Agent's thought process
60
+ )
61
+
62
+ # 7. Run the Agent
63
+ print("\n--- Running Agent Test ---")
64
+ response = agent.run("Check the current price of bitcoin.")
65
+
66
+ print("\n--- Agent Final Response ---")
67
+ print(response)
68
+
69
+ print("\nAgentExecutor and initialize_agent imported successfully for v0.3.0!")