Skip to content

Commit

Permalink
1. Add plugin: for obtaining the current time based on function call.
Browse files Browse the repository at this point in the history
2. Add plugin: to obtain the current version of the robot based on a function call.
  • Loading branch information
yym68686 committed Dec 18, 2023
1 parent 29a1954 commit 917c463
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 53 deletions.
15 changes: 15 additions & 0 deletions utils/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,23 @@ def check_json(json_data):
json_data += '"}'
return json_data

def get_date_time_weekday():
import datetime

now = datetime.datetime.now()
weekday = now.weekday()
weekday_str = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][weekday]
return "今天是:" + str(now.date()) + ",现在的时间是:" + str(now.time()) + "," + weekday_str

def get_version_info():
import subprocess
result = subprocess.run(['git', 'log', '-1'], stdout=subprocess.PIPE)
output = result.stdout.decode()
return output

if __name__ == "__main__":
os.system("clear")
print(get_version_info())

# from langchain.agents import get_all_tool_names
# print(get_all_tool_names())
Expand Down
12 changes: 9 additions & 3 deletions utils/chatgpt2api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Set

import config
from utils.agent import Web_crawler, get_search_results, cut_message, get_url_text_list, get_text_token_len, check_json
from utils.agent import Web_crawler, get_search_results, cut_message, get_url_text_list, get_text_token_len, check_json, get_date_time_weekday, get_version_info
from utils.function_call import function_call_list

def get_filtered_keys_from_object(obj: object, *keys: str) -> Set[str]:
Expand Down Expand Up @@ -200,8 +200,6 @@ def ask_stream(
# print(repr(self.conversation.Conversation(convo_id)))
# print("total tokens:", self.get_token_count(convo_id))



class Imagebot:
def __init__(
self,
Expand Down Expand Up @@ -510,6 +508,8 @@ def get_post_body(
if config.SEARCH_USE_GPT:
json_post_body["functions"].append(function_call_list["web_search"])
json_post_body["functions"].append(function_call_list["url_fetch"])
json_post_body["functions"].append(function_call_list["today"])
json_post_body["functions"].append(function_call_list["vresion"])

return json_post_body

Expand Down Expand Up @@ -637,6 +637,12 @@ def ask_stream(
print("\n\nurl", url)
function_response = Web_crawler(url)
function_response, text_len = cut_message(function_response, function_call_max_tokens)
if function_call_name == "get_date_time_weekday":
function_response = eval(function_call_name)()
function_response, text_len = cut_message(function_response, function_call_max_tokens)
if function_call_name == "get_version_info":
function_response = eval(function_call_name)()
function_response, text_len = cut_message(function_response, function_call_max_tokens)
else:
function_response = "抱歉,直接告诉用户,无法找到相关信息"
response_role = "function"
Expand Down
117 changes: 67 additions & 50 deletions utils/function_call.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,70 @@
function_call_list = {
"base": {
"functions": [],
"function_call": "auto"
},
"current_weather": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
function_call_list = \
{
"base": {
"functions": [],
"function_call": "auto"
},
"current_weather": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location"
]
}
},
"required": ["location"]
}
},
"web_search": {
"name": "get_search_results",
"description": "Search Google to enhance knowledge.",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The prompt to search."
},
"web_search": {
"name": "get_search_results",
"description": "Search Google to enhance knowledge.",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The prompt to search."
}
},
"required": [
"prompt"
]
}
},
"required": ["prompt"]
}
},
"url_fetch": {
"name": "get_url_content",
"description": "Get the webpage content of a URL",
"parameters": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "the URL to request"
},
"url_fetch": {
"name": "get_url_content",
"description": "Get the webpage content of a URL",
"parameters": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "the URL to request"
}
},
"required": [
"url"
]
}
},
"required": ["url"]
}
},
}

},
"today": {
"name": "get_date_time_weekday",
"description": "Get the current time, date, and day of the week"
},
"vresion": {
"name": "get_version_info",
"description": "Get version information"
},
}

0 comments on commit 917c463

Please sign in to comment.