lorashen commited on
Commit
0d921c7
·
verified ·
1 Parent(s): a1ec62c

upload agentlite example

Browse files
examples/agentlite/example/ExecAction.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import wikipedia
4
+ import duckduckgo_search
5
+
6
+ from agentlite.actions.BaseAction import BaseAction
7
+ import requests
8
+ import json
9
+
10
+ class ExecAction(BaseAction):
11
+ def __init__(self) -> None:
12
+ #action_name = "request"
13
+ #action_desc = "Using this action to request servers."
14
+ #params_doc = {"url": "the request url.","query":"the request content, json format."}
15
+ super().__init__(
16
+ action_name="request", action_desc="Using this action to request servers.", params_doc={"url": "the request url.","query":"if news or weather or qa or stock or currency server, then empty query. json format, like this{\"intent\": \"calendar_remove\", \"iid\": \"7890\", \"event_name\": \"haircut appointment\", \"date\": \"2024-11-20\"}"},
17
+ )
18
+ print(self.action_name)
19
+
20
+ def __call__(self, url,query):
21
+ if url.find("mediastack")!=-1 or url.find("meteo")!=-1 or url.find("marketstack")!=-1:
22
+ response=requests.get(url)
23
+ print(response.text)
24
+ else:
25
+ response = requests.post(url, data=query)
26
+
27
+ # 打印返回结果
28
+ print(response.text)
29
+ result=""
30
+ if response.status_code >= 400:
31
+ print(query)#["iid"])
32
+ result="code "+str(response.status_code)+"."
33
+ else:
34
+ pass
35
+ '''
36
+ res=json.loads(response.text)
37
+ if res["data"]["response"]=="true":
38
+ count+=1
39
+ result="true"
40
+ else:
41
+ print(query)#["iid"])
42
+ result="false"
43
+ print(res["data"]["response"])
44
+ '''
45
+ return response.text
examples/agentlite/example/ExecAgent.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from typing import List
4
+ from ExecAction import ExecAction
5
+ from agentlite.actions import BaseAction, FinishAct, ThinkAct
6
+ from agentlite.agents import BaseAgent
7
+ from agentlite.commons import TaskPackage
8
+ from agentlite.commons import AgentAct
9
+ from agentlite.llm.agent_llms import get_llm_backend, LLMConfig
10
+ from agentlite.llm.agent_llms import BaseLLM
11
+ from agentlite.logging.terminal_logger import AgentLogger
12
+ agent_logger = AgentLogger(PROMPT_DEBUG_FLAG=False)
13
+ # LAM_URL = os.environ["LAM_URL"]
14
+ # print(LAM_URL)
15
+ # llm_config = LLMConfig(
16
+ # {
17
+ # "llm_name": "xlam_v2",
18
+ # "temperature": 0.0,
19
+ # "base_url": LAM_URL,
20
+ # "api_key": "EMPTY"
21
+ # }
22
+ # )
23
+ llm_name = "gpt-4"#3.5-turbo"#-16k"#-0613"#gpt-4"
24
+ llm_config = LLMConfig({"llm_name": llm_name, "temperature": 0.0,
25
+ "api_key": "",})
26
+ llm = get_llm_backend(llm_config)
27
+ class ExecAgent(BaseAgent):
28
+ def __init__(
29
+ self, llm: BaseLLM, actions: List[BaseAction] = [ExecAction()], **kwargs
30
+ ):
31
+ name = "ExecAgent"
32
+ role = "read the params, and send and receive the requests. iid should also be string. choose the url from the servers' url list:\
33
+ qa server is http://api.serpstack.com/search?access_key={key}&query={query}\n\
34
+ news query server is http://api.mediastack.com/v1/news?access_key={key}&keywords={keyword}&date={date}&sort=published_desc\n\
35
+ news subscription server http://127.0.0.1:3020/news,intent(news_subscription),iid,news_topic,\
36
+ weather server first request https://geocoding-api.open-meteo.com/v1/search?name={place_name}&count=10&language=en&format=json to get latitude and latitude, then request https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=temperature_2m_max,temperature_2m_min,sunrise,sunset,uv_index_max,rain_sum,showers_sum,snowfall_sum,wind_speed_10m_max\n\
37
+ stock server is first to get the stock symbol http://api.serpstack.com/search? access_key = {key}& query = {name} stock symbol , then request to this server http://api.marketstack.com/v1/eod? access_key = {key}& symbols = {symbol}&limit=5\n\
38
+ currency server is https://www.amdoren.com/api/currency.php?api_key={key}&from={currency}&to={currency2}&amount={amount}\n\
39
+ http://127.0.0.1:3000/alarm, intent(alarm_query,alarm_set),iid,event_name,descriptor,time,from_time,to_time,\
40
+ http://127.0.0.1:3001/audiobook,intent(play_audiobook), iid,player_setting,house_place,media_type,descriptor,audiobook_name,author_name,\
41
+ http://127.0.0.1:3002/calendar,intent(calendar_query,calendar_remove,calendar_set),iid,event_name,descriptor,person,relation,date,time,from_time,to_time,\
42
+ http://127.0.0.1:3003/cooking,intent(cooking_recipe),iid,food_type,descriptor,\
43
+ http://127.0.0.1:3004/datetime,intent(datetime_convert,datetime_query),iid,place_name,descriptor,time_zone,time_zone2,date,time,time2,\
44
+ http://127.0.0.1:3005/email,intent(email_query,email_sendemail),iid,setting,person,to_person,from_person,relation,to_relation,from_relation,email_folder,time,date,email_address,app_name,query,content,personal_info,\
45
+ http://127.0.0.1:3006/game,intent(play_game),iid,game_name,\
46
+ http://127.0.0.1:3007/iot,intent(iot_coffee,iot_hue_lightcolor,iot_hue_lightother,iot_hue_lightdim,iot_hue_lightup,audio_volume_mute,iot_hue_lightoff,audio_volume_up,iot_wemo_off,audio_volume_other,iot_cleaning,iot_wemo_on,audio_volume_down),iid,device_type,house_place,time,color_type,change_amount,change_to,item_name,setting,\
47
+ http://127.0.0.1:3008/lists,intent(lists_query,lists_remove,lists_createoradd),iid,list_name,item_name,descriptor,time,date,\
48
+ http://127.0.0.1:3009/music,intent(play_music,music_likeness,playlists_createoradd,music_settings,music_dislikeness,music_query),iid,player_setting,descriptor,artist_name,song_name,playlist_name,music_genre,query,\
49
+ http://127.0.0.1:3010/phone,intent(phone_text,phone_notification),iid,device_type,event_name,text,\
50
+ http://127.0.0.1:3011/podcasts,intent(play_podcasts),iid,podcast_name,player_setting,podcast_descriptor,\
51
+ http://127.0.0.1:3013/radio,intent(play_radio,radio_query),iid,radio_name,app_name,person_name,music_genre,device_type,house_place,player_setting,descriptor,query,time,\
52
+ http://127.0.0.1:3014/recommendation,intent(recommendation_events,recommendation_movies,recommendation_locations),iid,business_type,food_type,movie_type,movie_name,date,place_name,event_name,descriptor,\
53
+ http://127.0.0.1:3015/social,intent(social_query,social_post),iid,media_type,person,business_name,content,date,descriptor,\
54
+ http://127.0.0.1:3017/takeaway,intent(takeaway_query,takeaway_order),iid,food_type,order_type,business_type,business_name,place_name,date,time,descriptor,\
55
+ http://127.0.0.1:3018/transport,intent(transport_taxi,transport_ticket,transport_query,transport_traffic),iid,transport_agency,transport_type,business_type,business_name,place_name,to_place_name,from_place_name,query,date,time,descriptor,\
56
+ "
57
+ super().__init__(
58
+ name=name, role=role, llm=llm, actions=actions, logger=agent_logger
59
+ )
60
+ self.__build_examples__()
61
+
62
+ def __build_examples__(self):
63
+ """
64
+ constructing the examples for agent working.
65
+ Each example is a successful action-obs chain of an agent.
66
+ those examples should cover all those api calls
67
+ """
68
+ # an example of search agent with wikipedia api call
69
+ # task
70
+ task = "intent:set_notification"
71
+
72
+
73
+ # 2. api call action and obs
74
+ act_params = {}
75
+ act_1 = AgentAct(name=ExecAction().action_name, params=act_params)
76
+ obs_1 = "OK"
77
+ '''
78
+ # 3. think action and obs
79
+ thought = "I find salesforce is Founded by former Oracle executive Marc Benioff in February 1999"
80
+ act_3 = AgentAct(name=ThinkAct.action_name, params={INNER_ACT_KEY: thought})
81
+ obs_3 = "OK"
82
+
83
+ # 4. finish action
84
+ answer = "February 1999"
85
+ act_4 = AgentAct(name=FinishAct.action_name, params={INNER_ACT_KEY: answer})
86
+ obs_4 = "Task Completed."
87
+
88
+ task_pack = TaskPackage(instruction=task)
89
+ act_obs = [(act_1, obs_1), (act_2, obs_2), (act_3, obs_3), (act_4, obs_4)]
90
+ self.add_example(task=task_pack, action_chain=act_obs)
91
+ '''
92
+ def test_search_agent():
93
+ llm_config_dict = {"llm_name": "gpt-4", "temperature": 0.1}
94
+ actions = [ExecAction()]
95
+ llm_config = LLMConfig(llm_config_dict)
96
+ # print(llm_config.__dict__)
97
+ llm = get_llm_backend(llm_config)
98
+ ## test the one-shot wikipedia search agent
99
+ labor_agent = ExecAgent(llm=llm)
100
+ # labor_agent = DuckSearchAgent(llm=llm)
101
+
102
+ test_task = "intent:set_notification, news_category:world news"
103
+ test_task_pack = TaskPackage(instruction=test_task)
104
+ response = labor_agent(test_task_pack)
105
+ print("response:", response)
106
+
107
+
108
+ if __name__ == "__main__":
109
+ test_search_agent()
110
+
111
+ '''
112
+ agent_actions = []#get_intent(),get_user_current_date(), get_user_current_location(), get_latitude_longitude()]#, get_weather_forcast()]
113
+ agent = BaseAgent(
114
+ name=agent_info["name"],
115
+ role=agent_info["role"],
116
+ llm=llm,
117
+ actions=agent_actions,
118
+ #reasoning_type="react",
119
+ )
120
+ prompt="the examples and results: turn up intent:audio_volume_up\nwhat's the current weather intent:weather_query\ni want you to remind me the next meeting with my girlfriend it will be at eight pm next sunday intent:calendar_set,event_name : meeting,relation:girlfriend, time : eight pm, date : next Sunday\ncancel alarm for tenth of march two thousand seventeen intent:alarm_remove,date : tenth of march two thousand seventeen\nhave you heard any good jokes lately intent:general_joke\nget it fast resolved intent:social_post\nalexa book me a train ticket for this afternoon to chicago intent:transport_ticket,transport_type : train, timeofday : this afternoon, place_name : chicago\ni did not want you to send that text yet wait until i say send intent:email_sendemail,setting:save\nwhat causes if i had junk food and alcohols intent:general_quirky,content:what causes if i had junk food and alcohols\nfind a recipe for a romantic dinner for two intent:cooking_recipe,food_descriptor: romantic dinner for two\nplease turn up the lights in this room intent:iot_hue_lightup,house_place:this room\nwhats happening in pop industry intent:news_query ,news_topic : pop industry\nis today the fourth or the fifth intent:datetime_query,date : today\nthe wemo plug should be turned off on intent:iot_wemo_off,device_type : wemo plug\nwhen i get home can you please order a pizza intent:takeaway_order,food_type : pizza\nfind the events intent:recommendation_events\nstart radio and go to frequency on one thousand and forty eight intent:play_radio,radio_name : one thousand and forty eight\ni want to listen arijit singh song once again intent:play_music,artist_name : arijit singh\nplease arrange to wake me up at three am alarm intent:alarm_set,time : three am\nstart dune from where i left off intent:play_audiobook,player_setting : resume, audiobook_name : dune\n please return the result of this sentence: "
121
+ FLAG_CONTINUE = True
122
+ while FLAG_CONTINUE:
123
+ input_text = input("Ask Intent Agent question:\n")
124
+ task = TaskPackage(instruction=prompt+"\n"+input_text)
125
+ agent(task)
126
+ if input("Do you want to continue? (y/n): ") == "n":
127
+ FLAG_CONTINUE = False
128
+ '''
examples/agentlite/example/IntentAgent.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from typing import List
4
+
5
+ from agentlite.actions import BaseAction, FinishAct, ThinkAct
6
+ from agentlite.agents import BaseAgent
7
+ from agentlite.commons import TaskPackage
8
+ from agentlite.llm.agent_llms import get_llm_backend, LLMConfig
9
+ from agentlite.llm.agent_llms import BaseLLM
10
+ from agentlite.logging.terminal_logger import AgentLogger
11
+ agent_logger = AgentLogger(PROMPT_DEBUG_FLAG=False)
12
+ # LAM_URL = os.environ["LAM_URL"]
13
+ # print(LAM_URL)
14
+ # llm_config = LLMConfig(
15
+ # {
16
+ # "llm_name": "xlam_v2",
17
+ # "temperature": 0.0,
18
+ # "base_url": LAM_URL,
19
+ # "api_key": "EMPTY"
20
+ # }
21
+ # )
22
+ llm_name = "gpt-4"#3.5-turbo"#-16k"#-0613"#gpt-4"
23
+ llm_config = LLMConfig({"llm_name": llm_name, "temperature": 0.0,
24
+ "api_key": "",})
25
+ llm = get_llm_backend(llm_config)
26
+ class IntentAgent(BaseAgent):
27
+ def __init__(
28
+ self, llm: BaseLLM, actions: List[BaseAction] = [], **kwargs
29
+ ):
30
+ name = "IntentAgent"
31
+ role = "read the examples and results, copy iid and predict intent for the sentence. for 'iid:7199,query:set the alarm to two pm' first predict the domain, as domain:alarm, then copy the iid from query,iid:7199, then the intent and slots, as the format: intent:alarm_set,time:two pm. the intents are calendar:calendar_set,calendar_remove,calendar_query\n\
32
+ lists:lists_query,lists_remove,lists_createoradd\n\
33
+ music:play_music,music_likeness,playlists_createoradd,music_settings,music_dislikeness,music_query\n\
34
+ news:news_query,news_subscription\n\
35
+ alarm:alarm_set,alarm_query,alarm_remove,alarm_change\n\
36
+ email:email_sendemail,email_query,email_querycontact,email_subscription,email_addcontact,email_remove\n\
37
+ iot:iot_hue_lightcolor,iot_hue_lightother,iot_coffee,iot_hue_lightdim,iot_hue_lightup,audio_volume_mute,iot_hue_lightoff,audio_volume_up,iot_wemo_off,audio_volume_other,iot_cleaning,iot_wemo_on,audio_volume_down\n\
38
+ weather:weather_query\n\
39
+ datetime:datetime_query,datetime_convert\n\
40
+ stock:qa_stock\n\
41
+ qa:qa_factoid,general_quirky,qa_definition,general_joke,qa_maths\n\
42
+ greet:general_greet\n\
43
+ currency:qa_currency\n\
44
+ transport:transport_taxi,transport_ticket,transport_query,transport_traffic\n\
45
+ recommendation:recommendation_events,recommendation_movies,recommendation_locations\n\
46
+ podcast:play_podcasts\n\
47
+ audiobook:play_audiobook\n\
48
+ radio:play_radio,radio_query\n\
49
+ takeaway:takeaway_query,takeaway_order\n\
50
+ social:social_query,social_post\n\
51
+ cooking:cooking_recipe\n\
52
+ phone:phone_text,phone_notification\n\
53
+ game:play_game\
54
+ "
55
+ super().__init__(
56
+ name=name, role=role, llm=llm, actions=actions, logger=agent_logger
57
+ )
58
+ #self.__build_examples__()
59
+
60
+ def __build_examples__(self):
61
+ """
62
+ constructing the examples for agent working.
63
+ Each example is a successful action-obs chain of an agent.
64
+ those examples should cover all those api calls
65
+ """
66
+ # an example of search agent with wikipedia api call
67
+ # task
68
+ task = "send the email to this new email address"
69
+
70
+ # 1. think action and obs
71
+ thought = "I should first figure out the intent"
72
+ act_1 = AgentAct(name=ThinkAct.action_name, params={INNER_ACT_KEY: thought})
73
+ obs_1 = ""
74
+ '''
75
+ agent_actions = []
76
+ agent = BaseAgent(
77
+ name=agent_info["name"],
78
+ role=agent_info["role"],
79
+ llm=llm,
80
+ actions=agent_actions,
81
+ #reasoning_type="react",
82
+ )
83
+ prompt="the examples and results: {"iid": "7499", "query": "turn up"} iid:7499,intent:audio_volume_up\n {"iid": "6811", "query": "what's the current weather"} iid:6811,intent:weather_query\ni want you to remind me the next meeting with my girlfriend it will be at eight pm next sunday intent:calendar_set,event_name : meeting,relation:girlfriend, time : eight pm, date : next Sunday\ncancel alarm for tenth of march two thousand seventeen intent:alarm_remove,date : tenth of march two thousand seventeen\nhave you heard any good jokes lately intent:general_joke\nget it fast resolved intent:social_post\nalexa book me a train ticket for this afternoon to chicago intent:transport_ticket,transport_type : train, timeofday : this afternoon, place_name : chicago\ni did not want you to send that text yet wait until i say send intent:email_sendemail,setting:save\nwhat causes if i had junk food and alcohols intent:general_quirky,content:what causes if i had junk food and alcohols\nfind a recipe for a romantic dinner for two intent:cooking_recipe,food_descriptor: romantic dinner for two\nplease turn up the lights in this room intent:iot_hue_lightup,house_place:this room\nwhats happening in pop industry intent:news_query ,news_topic : pop industry\nis today the fourth or the fifth intent:datetime_query,date : today\nthe wemo plug should be turned off on intent:iot_wemo_off,device_type : wemo plug\nwhen i get home can you please order a pizza intent:takeaway_order,food_type : pizza\nfind the events intent:recommendation_events\nstart radio and go to frequency on one thousand and forty eight intent:play_radio,radio_name : one thousand and forty eight\ni want to listen arijit singh song once again intent:play_music,artist_name : arijit singh\nplease arrange to wake me up at three am alarm intent:alarm_set,time : three am\nstart dune from where i left off intent:play_audiobook,player_setting : resume, audiobook_name : dune\n please return the result of this sentence: "
84
+ FLAG_CONTINUE = True
85
+ while FLAG_CONTINUE:
86
+ input_text = input("Ask Intent Agent question:\n")
87
+ task = TaskPackage(instruction=prompt+"\n"+input_text)
88
+ agent(task)
89
+ if input("Do you want to continue? (y/n): ") == "n":
90
+ FLAG_CONTINUE = False
91
+ '''
examples/agentlite/example/LocationAgent.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from typing import List
4
+ from intent_actions import get_intent,get_user_current_date, get_user_current_location, get_latitude_longitude
5
+ from agentlite.actions import BaseAction, FinishAct, ThinkAct
6
+ from agentlite.agents import BaseAgent
7
+ from agentlite.commons import TaskPackage
8
+ from agentlite.llm.agent_llms import get_llm_backend, LLMConfig
9
+ from agentlite.llm.agent_llms import BaseLLM
10
+ from agentlite.logging.terminal_logger import AgentLogger
11
+ agent_logger = AgentLogger(PROMPT_DEBUG_FLAG=False)
12
+ # LAM_URL = os.environ["LAM_URL"]
13
+ # print(LAM_URL)
14
+ # llm_config = LLMConfig(
15
+ # {
16
+ # "llm_name": "xlam_v2",
17
+ # "temperature": 0.0,
18
+ # "base_url": LAM_URL,
19
+ # "api_key": "EMPTY"
20
+ # }
21
+ # )
22
+ llm_name = "gpt-4"#3.5-turbo"#-16k"#-0613"#gpt-4"
23
+ llm_config = LLMConfig({"llm_name": llm_name, "temperature": 0.0,
24
+ "api_key": "",})
25
+ llm = get_llm_backend(llm_config)
26
+ class LocationAgent(BaseAgent):
27
+ def __init__(
28
+ self, llm: BaseLLM, actions: List[BaseAction] = [], **kwargs
29
+ ):
30
+ name = "LocationAgent"
31
+ role = "read the location params, and convert to formated location. if location is in house, do not need to resolve. current location is new york."
32
+ super().__init__(
33
+ name=name, role=role, llm=llm, actions=actions, logger=agent_logger
34
+ )
35
+ #self.__build_examples__()
36
+
37
+ def __build_examples__(self):
38
+ """
39
+ constructing the examples for agent working.
40
+ Each example is a successful action-obs chain of an agent.
41
+ those examples should cover all those api calls
42
+ """
43
+ # an example of search agent with wikipedia api call
44
+ # task
45
+ task = "place_name:ny"
46
+
47
+ # 1. think action and obs
48
+ thought = "I should first figure out ny"
49
+ act_1 = AgentAct(name=ThinkAct.action_name, params={INNER_ACT_KEY: thought})
50
+ obs_1 = ""
51
+ '''
52
+ agent_actions = []#get_intent(),get_user_current_date(), get_user_current_location(), get_latitude_longitude()]#, get_weather_forcast()]
53
+ agent = BaseAgent(
54
+ name=agent_info["name"],
55
+ role=agent_info["role"],
56
+ llm=llm,
57
+ actions=agent_actions,
58
+ #reasoning_type="react",
59
+ )
60
+ prompt="the examples and results: turn up intent:audio_volume_up\nwhat's the current weather intent:weather_query\ni want you to remind me the next meeting with my girlfriend it will be at eight pm next sunday intent:calendar_set,event_name : meeting,relation:girlfriend, time : eight pm, date : next Sunday\ncancel alarm for tenth of march two thousand seventeen intent:alarm_remove,date : tenth of march two thousand seventeen\nhave you heard any good jokes lately intent:general_joke\nget it fast resolved intent:social_post\nalexa book me a train ticket for this afternoon to chicago intent:transport_ticket,transport_type : train, timeofday : this afternoon, place_name : chicago\ni did not want you to send that text yet wait until i say send intent:email_sendemail,setting:save\nwhat causes if i had junk food and alcohols intent:general_quirky,content:what causes if i had junk food and alcohols\nfind a recipe for a romantic dinner for two intent:cooking_recipe,food_descriptor: romantic dinner for two\nplease turn up the lights in this room intent:iot_hue_lightup,house_place:this room\nwhats happening in pop industry intent:news_query ,news_topic : pop industry\nis today the fourth or the fifth intent:datetime_query,date : today\nthe wemo plug should be turned off on intent:iot_wemo_off,device_type : wemo plug\nwhen i get home can you please order a pizza intent:takeaway_order,food_type : pizza\nfind the events intent:recommendation_events\nstart radio and go to frequency on one thousand and forty eight intent:play_radio,radio_name : one thousand and forty eight\ni want to listen arijit singh song once again intent:play_music,artist_name : arijit singh\nplease arrange to wake me up at three am alarm intent:alarm_set,time : three am\nstart dune from where i left off intent:play_audiobook,player_setting : resume, audiobook_name : dune\n please return the result of this sentence: "
61
+ FLAG_CONTINUE = True
62
+ while FLAG_CONTINUE:
63
+ input_text = input("Ask Intent Agent question:\n")
64
+ task = TaskPackage(instruction=prompt+"\n"+input_text)
65
+ agent(task)
66
+ if input("Do you want to continue? (y/n): ") == "n":
67
+ FLAG_CONTINUE = False
68
+ '''
examples/agentlite/example/TimeAction.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+
4
+ from agentlite.actions.BaseAction import BaseAction
5
+ from datetime import date, datetime
6
+
7
+ class TimeAction(BaseAction):
8
+ def __init__(self) -> None:
9
+ action_name = "Time_Act"
10
+ action_desc = "Using this action to get time."
11
+ params_doc = {"query": "the search string. be simple."}
12
+ super().__init__(
13
+ action_name=action_name, action_desc=action_desc, params_doc=params_doc,
14
+ )
15
+
16
+ def __call__(self, query):
17
+ results = query
18
+ return results
19
+
20
+ class CurDateAction(BaseAction):
21
+ def __init__(self) -> None:
22
+ action_name = "CurDate_Act"
23
+ action_desc = "Using this action to get current date."
24
+ params_doc = {"query": "the search string. be simple."}
25
+ super().__init__(
26
+ action_name=action_name, action_desc=action_desc, params_doc=params_doc,
27
+ )
28
+
29
+ def __call__(self, query):
30
+ results = date.today()
31
+ return str(results)
32
+
33
+ class CurTimeAction(BaseAction):
34
+ def __init__(self) -> None:
35
+ action_name = "CurTime_Act"
36
+ action_desc = "Using this action to get current time."
37
+ params_doc = {""}
38
+ super().__init__(
39
+ action_name=action_name, action_desc=action_desc, params_doc=params_doc,
40
+ )
41
+
42
+ def __call__(self, query):
43
+ results = datetime.now()
44
+ return results
examples/agentlite/example/TimeAgent.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from typing import List
4
+ from intent_actions import get_intent,get_user_current_date, get_user_current_location, get_latitude_longitude
5
+ from agentlite.actions import BaseAction, FinishAct, ThinkAct
6
+ from agentlite.agents import BaseAgent
7
+ from agentlite.commons import TaskPackage
8
+ from agentlite.llm.agent_llms import get_llm_backend, LLMConfig
9
+ from agentlite.llm.agent_llms import BaseLLM
10
+ from intent_actions import get_intent,get_user_current_date, get_user_current_location, get_latitude_longitude
11
+
12
+ from TimeAction import TimeAction,CurDateAction
13
+ from agentlite.logging.terminal_logger import AgentLogger
14
+ agent_logger = AgentLogger(PROMPT_DEBUG_FLAG=False)
15
+ # LAM_URL = os.environ["LAM_URL"]
16
+ # print(LAM_URL)
17
+ # llm_config = LLMConfig(
18
+ # {
19
+ # "llm_name": "xlam_v2",
20
+ # "temperature": 0.0,
21
+ # "base_url": LAM_URL,
22
+ # "api_key": "EMPTY"
23
+ # }
24
+ # )
25
+ llm_name = "gpt-4"#3.5-turbo"#-16k"#-0613"#gpt-4"
26
+ llm_config = LLMConfig({"llm_name": llm_name, "temperature": 0.0,
27
+ "api_key": "",})
28
+ llm = get_llm_backend(llm_config)
29
+ class TimeAgent(BaseAgent):
30
+ def __init__(
31
+ self, llm: BaseLLM, actions: List[BaseAction] = [TimeAction(),CurDateAction()], **kwargs
32
+ ):
33
+ name = "TimeAgent"
34
+ role = "read the time params, and convert to formated time with TimeAction. if has date, call the CurDateAction to get date, format should be 2024-11-20. the time is 10:00. if has time, the time format should be 10:00"
35
+ super().__init__(
36
+ name=name, role=role, llm=llm, actions=actions, logger=agent_logger
37
+ )
38
+ #self.__build_examples__()
39
+
40
+ def __build_examples__(self):
41
+ """
42
+ constructing the examples for agent working.
43
+ Each example is a successful action-obs chain of an agent.
44
+ those examples should cover all those api calls
45
+ """
46
+ # an example of search agent with wikipedia api call
47
+ # task
48
+ task = "send the email to this new email address"
49
+
50
+ # 1. think action and obs
51
+ thought = "I should first figure out the intent"
52
+ act_1 = AgentAct(name=ThinkAct.action_name, params={INNER_ACT_KEY: thought})
53
+ obs_1 = ""
54
+ '''
55
+ agent_actions = []#get_intent(),get_user_current_date(), get_user_current_location(), get_latitude_longitude()]#, get_weather_forcast()]
56
+ agent = BaseAgent(
57
+ name=agent_info["name"],
58
+ role=agent_info["role"],
59
+ llm=llm,
60
+ actions=agent_actions,
61
+ #reasoning_type="react",
62
+ )
63
+ prompt="the examples and results: turn up intent:audio_volume_up\nwhat's the current weather intent:weather_query\ni want you to remind me the next meeting with my girlfriend it will be at eight pm next sunday intent:calendar_set,event_name : meeting,relation:girlfriend, time : eight pm, date : next Sunday\ncancel alarm for tenth of march two thousand seventeen intent:alarm_remove,date : tenth of march two thousand seventeen\nhave you heard any good jokes lately intent:general_joke\nget it fast resolved intent:social_post\nalexa book me a train ticket for this afternoon to chicago intent:transport_ticket,transport_type : train, timeofday : this afternoon, place_name : chicago\ni did not want you to send that text yet wait until i say send intent:email_sendemail,setting:save\nwhat causes if i had junk food and alcohols intent:general_quirky,content:what causes if i had junk food and alcohols\nfind a recipe for a romantic dinner for two intent:cooking_recipe,food_descriptor: romantic dinner for two\nplease turn up the lights in this room intent:iot_hue_lightup,house_place:this room\nwhats happening in pop industry intent:news_query ,news_topic : pop industry\nis today the fourth or the fifth intent:datetime_query,date : today\nthe wemo plug should be turned off on intent:iot_wemo_off,device_type : wemo plug\nwhen i get home can you please order a pizza intent:takeaway_order,food_type : pizza\nfind the events intent:recommendation_events\nstart radio and go to frequency on one thousand and forty eight intent:play_radio,radio_name : one thousand and forty eight\ni want to listen arijit singh song once again intent:play_music,artist_name : arijit singh\nplease arrange to wake me up at three am alarm intent:alarm_set,time : three am\nstart dune from where i left off intent:play_audiobook,player_setting : resume, audiobook_name : dune\n please return the result of this sentence: "
64
+ FLAG_CONTINUE = True
65
+ while FLAG_CONTINUE:
66
+ input_text = input("Ask Intent Agent question:\n")
67
+ task = TaskPackage(instruction=prompt+"\n"+input_text)
68
+ agent(task)
69
+ if input("Do you want to continue? (y/n): ") == "n":
70
+ FLAG_CONTINUE = False
71
+ '''
examples/agentlite/example/iot_manager.py CHANGED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # get llm backend
2
+ from agentlite.llm.agent_llms import get_llm_backend
3
+ from agentlite.llm.LLMConfig import LLMConfig
4
+ import csv
5
+ import sys
6
+ import json
7
+
8
+ llm_config_dict = {
9
+ "llm_name": "gpt-4",#3.5-turbo-16k-0613",
10
+ "temperature": 0.0,
11
+ "api_key": "",
12
+ }
13
+ llm_config = LLMConfig(llm_config_dict)
14
+ llm = get_llm_backend(llm_config)
15
+
16
+ # define the individual agents
17
+ from IntentAgent import IntentAgent
18
+ from TimeAgent import TimeAgent
19
+
20
+
21
+ from LocationAgent import LocationAgent
22
+ from ExecAgent import ExecAgent
23
+
24
+
25
+ intent_agent = IntentAgent(llm)
26
+ time_agent = TimeAgent(llm)
27
+ location_agent = LocationAgent(llm)
28
+ excution_agent = ExecAgent(llm)
29
+
30
+
31
+ # define the manager agent
32
+ from agentlite.agents import ManagerAgent
33
+
34
+ manager_agent_info = {
35
+ "name": "whole_manager",
36
+ "role": "you are controlling smart home system, you have intent_agent, time_agent, location_agent, and excution_agent to complete the user's task. You should first use intent_agent to complete the intent prediction. Then if the result has time or location params, please try to ask time_agent or location_agent to solve the time and location. If has currency name, you should convert the currency name to the name in the currency map. At last you should use excution_agent to send and receive request from other servers such as weather server and response to user to finalize the task. If the server's response is need further information, need to response to the user that need infomation, and your task is finished.",
37
+ }
38
+ iot_manager = ManagerAgent(
39
+ llm,
40
+ manager_agent_info["name"],
41
+ manager_agent_info["role"],
42
+ TeamAgents=[intent_agent, time_agent,location_agent,currency_agent,excution_agent],
43
+ )
44
+
45
+ # test the manager agent with TaskPackage
46
+ from agentlite.commons import TaskPackage
47
+
48
+ def read_data(file_path):
49
+ queries=[]
50
+ with open(file_path, newline='') as csvfile:
51
+ spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
52
+ count=0
53
+ for row in spamreader:
54
+ if count==0:
55
+ count+=1
56
+ continue
57
+ #print(row[2])
58
+ #print(len(row))
59
+ iid=row[0]
60
+ query={"iid":iid,"query":row[1]}#,"slots":slot,"domain":row[4]}
61
+ queries.append(query)
62
+ return queries
63
+
64
+ if __name__=="__main__":
65
+ tasks=read_data("~/data/test.csv")
66
+ #print(tasks)
67
+ index = int(sys.argv[1])
68
+ tasks=tasks[index:index+1]
69
+ for task in tasks:
70
+ #for task in tasks:
71
+ test_task = json.dumps(task)
72
+ test_task_pack = TaskPackage(instruction=test_task, task_creator="User")
73
+ response = iot_manager(test_task_pack)
74
+ print(response)
75
+ #test_task = "set notification from world news"
76
+ '''
77
+ test_task = "what is the weather today"
78
+ test_task_pack = TaskPackage(instruction=test_task, task_creator="User")
79
+ response = iot_manager(test_task_pack)
80
+ print(response)