AV-AV commited on
Commit
f2b0c4b
·
verified ·
1 Parent(s): 9a173cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -63
app.py CHANGED
@@ -1,63 +1,133 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()