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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Import necessary modules
|
| 3 |
+
import os
|
| 4 |
+
from langchain_community.utilities.alpha_vantage import AlphaVantageAPIWrapper
|
| 5 |
+
|
| 6 |
+
# Read API keys from files
|
| 7 |
+
with open('mykey.txt', 'r') as file:
|
| 8 |
+
openai_key = file.read()
|
| 9 |
+
|
| 10 |
+
with open('alpha_key.txt', 'r') as file:
|
| 11 |
+
alpha_key = file.read()
|
| 12 |
+
|
| 13 |
+
# Set environment variables for API keys
|
| 14 |
+
os.environ['OPENAI_API_KEY'] = openai_key
|
| 15 |
+
os.environ["ALPHAVANTAGE_API_KEY"] = alpha_key # 25 requests per day in free option
|
| 16 |
+
|
| 17 |
+
# Create an instance of the AlphaVantageAPIWrapper
|
| 18 |
+
alpha_vantage = AlphaVantageAPIWrapper()
|
| 19 |
+
|
| 20 |
+
# Get the last 100 days prices for the stock symbol "AAPL"
|
| 21 |
+
alpha_vantage._get_time_series_daily("AAPL")
|
| 22 |
+
|
| 23 |
+
# Import necessary modules for creating a chatbot
|
| 24 |
+
from langchain.agents import tool
|
| 25 |
+
from langchain.chat_models import ChatOpenAI
|
| 26 |
+
from langchain.prompts import ChatPromptTemplate
|
| 27 |
+
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
| 28 |
+
from langchain.agents import AgentExecutor
|
| 29 |
+
from langchain.schema.runnable import RunnablePassthrough
|
| 30 |
+
from langchain.agents.format_scratchpad import format_to_openai_functions
|
| 31 |
+
from langchain.prompts import MessagesPlaceholder
|
| 32 |
+
from langchain.memory import ConversationBufferMemory
|
| 33 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Import necessary modules for creating additional tools
|
| 37 |
+
import wikipedia
|
| 38 |
+
import datetime
|
| 39 |
+
import requests
|
| 40 |
+
|
| 41 |
+
@tool
|
| 42 |
+
def search_wikipedia(query: str) -> str:
|
| 43 |
+
"""Run Wikipedia search and get page summaries."""
|
| 44 |
+
page_titles = wikipedia.search(query)
|
| 45 |
+
summaries = []
|
| 46 |
+
for page_title in page_titles[:1]:
|
| 47 |
+
try:
|
| 48 |
+
wiki_page = wikipedia.page(title=page_title, auto_suggest=False)
|
| 49 |
+
summaries.append(f"Page: {page_title}\nSummary: {wiki_page.summary}")
|
| 50 |
+
except (
|
| 51 |
+
self.wiki_client.exceptions.PageError,
|
| 52 |
+
self.wiki_client.exceptions.DisambiguationError,
|
| 53 |
+
):
|
| 54 |
+
pass
|
| 55 |
+
if not summaries:
|
| 56 |
+
return "No good Wikipedia Search Result was found"
|
| 57 |
+
return "\n\n".join(summaries)
|
| 58 |
+
|
| 59 |
+
@tool
|
| 60 |
+
def get_current_temperature(latitude: float, longitude: float) -> dict:
|
| 61 |
+
"""Fetch current temperature for given coordinates."""
|
| 62 |
+
|
| 63 |
+
BASE_URL = "https://api.open-meteo.com/v1/forecast"
|
| 64 |
+
|
| 65 |
+
# Parameters for the request
|
| 66 |
+
params = {
|
| 67 |
+
'latitude': latitude,
|
| 68 |
+
'longitude': longitude,
|
| 69 |
+
'hourly': 'temperature_2m',
|
| 70 |
+
'forecast_days': 1,
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
# Make the request
|
| 74 |
+
response = requests.get(BASE_URL, params=params)
|
| 75 |
+
|
| 76 |
+
if response.status_code == 200:
|
| 77 |
+
results = response.json()
|
| 78 |
+
else:
|
| 79 |
+
raise Exception(f"API Request failed with status code: {response.status_code}")
|
| 80 |
+
|
| 81 |
+
current_utc_time = datetime.datetime.utcnow()
|
| 82 |
+
time_list = [datetime.datetime.fromisoformat(time_str.replace('Z', '+00:00')) for time_str in results['hourly']['time']]
|
| 83 |
+
temperature_list = results['hourly']['temperature_2m']
|
| 84 |
+
|
| 85 |
+
closest_time_index = min(range(len(time_list)), key=lambda i: abs(time_list[i] - current_utc_time))
|
| 86 |
+
current_temperature = temperature_list[closest_time_index]
|
| 87 |
+
|
| 88 |
+
return f'The current temperature is {current_temperature}°C'
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Update the prompt template to include multiple tools
|
| 92 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 93 |
+
("system", "You are a helpful assistant"),
|
| 94 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 95 |
+
("user", "{input}"),
|
| 96 |
+
MessagesPlaceholder(variable_name="agent_scratchpad")
|
| 97 |
+
])
|
| 98 |
+
|
| 99 |
+
# Convert the additional functions to OpenAI functions
|
| 100 |
+
functions = [convert_to_openai_function(f) for f in [get_stock_price, get_current_temperature, search_wikipedia]]
|
| 101 |
+
|
| 102 |
+
# Create a new model instance with the updated functions
|
| 103 |
+
model = ChatOpenAI(temperature=0, model='gpt-4o').bind(functions=functions)
|
| 104 |
+
|
| 105 |
+
# Update the agent chain with the new model and functions
|
| 106 |
+
agent_chain = RunnablePassthrough.assign(
|
| 107 |
+
agent_scratchpad= lambda x: format_to_openai_functions(x["intermediate_steps"])
|
| 108 |
+
) | prompt | model | OpenAIFunctionsAgentOutputParser()
|
| 109 |
+
|
| 110 |
+
# Update the memory buffer
|
| 111 |
+
memory = ConversationBufferWindowMemory(return_messages=True, memory_key="chat_history", k =5, output_key="output")
|
| 112 |
+
tools = [get_stock_price, search_wikipedia, get_current_temperature]
|
| 113 |
+
|
| 114 |
+
agent_executor = AgentExecutor(agent=agent_chain, tools=tools, verbose=False, memory=memory, return_intermediate_steps=True)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def my_chatbot(prompt: str):
|
| 118 |
+
reply = agent_executor.invoke({"input": prompt})
|
| 119 |
+
if len(reply['intermediate_steps'])==0:
|
| 120 |
+
tool = 'None'
|
| 121 |
+
else:
|
| 122 |
+
tool = reply['intermediate_steps'][0][0].tool
|
| 123 |
+
|
| 124 |
+
return tool, reply['output']
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
demo = gr.Interface(fn=my_chatbot,
|
| 129 |
+
inputs=[gr.Textbox(label="Query", lines=3)],
|
| 130 |
+
outputs=[gr.Textbox(label="Tool", lines = 1), gr.Textbox(label="Tool", lines = 10)],
|
| 131 |
+
title="Demo Agent",
|
| 132 |
+
description= "Flag responses where inappropriate tool is used")
|
| 133 |
+
demo.launch()
|