This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-advanced.py
More file actions
78 lines (62 loc) · 2.18 KB
/
example-advanced.py
File metadata and controls
78 lines (62 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import datetime
import os
import dotenv
import requests
from gptconnect import GPTConnect, GPTFunction, GPTFunctionHandler, Params, Property
dotenv.load_dotenv()
ai = GPTConnect(token=os.environ.get("TOKEN"), model="gpt-3.5-turbo-0613")
@GPTFunctionHandler()
def custom_function_handler(function, args):
# Please make sure to update or remove this code when adding new functions
if function.__name__ in ["ping_hostname", "get_time"]:
return function(args)
else:
return "The function called is invalid. Please let the user know this operation failed."
@GPTFunction(
group="general_commands",
description="Ping a hostname",
params=Params(
properties={"hostname": Property(str, "The hostname to ping")},
required=["hostname"],
),
# You can also use the standard OpenAI params dictionary format, like this:
# params={
# "type": "object",
# "properties": {
# "hostname": {
# "type": "string",
# "description": "The hostname to ping",
# },
# },
# "required": ["hostname"],
# },
)
def ping_hostname(args):
print(f"Pinging hostname {args.get('hostname')}...")
url = f"https://{args.get('hostname')}"
try:
response = requests.get(url)
return f"Ping was successful. Response code was {response.status_code}."
except requests.exceptions.ConnectionError:
return f"The hostname {args.get('hostname')} could not be pinged."
@GPTFunction(
group="general_commands",
description="Get the current time",
params=Params(
properties={},
required=[],
),
)
def get_time(args):
formatted_time = datetime.datetime.now().strftime("%H:%M")
return formatted_time
print(ai.call(prompt="Ping the hostname github", function_group="general_commands"))
# Output:
# Pinging hostname github.com...
# {
# 'content': 'The hostname "github.com" was successfully pinged with a response code of 200.',
# 'function_called': 'ping_hostname'
# }
print(ai.call(prompt="What's the time?", function_group="general_commands"))
# Output:
# {'content': 'The current time is 13:18.', 'function_called': 'get_time'}