Skip to content

Commit

Permalink
added HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacker committed Oct 29, 2023
1 parent 1651b3a commit e153311
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
34 changes: 34 additions & 0 deletions memgpt/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pickle
import math
import os
import requests
import json
import threading

Expand Down Expand Up @@ -251,6 +252,7 @@ def init_avail_functions(self):
# extras
"read_from_text_file": self.read_from_text_file,
"append_to_text_file": self.append_to_text_file,
"http_request": self.http_request,
}

@property
Expand Down Expand Up @@ -772,6 +774,38 @@ def append_to_text_file(self, filename, content):
with open(filename, "a") as file:
file.write(content + "\n")

def http_request(self, method, url, payload_json=None):
"""
Makes an HTTP request based on the specified method, URL, and JSON payload.
Args:
method (str): The HTTP method (e.g., 'GET', 'POST').
url (str): The URL for the request.
payload_json (str): A JSON string representing the request payload.
Returns:
dict: The response from the HTTP request.
"""
try:
headers = {"Content-Type": "application/json"}

# For GET requests, ignore the payload
if method.upper() == "GET":
print(f"[HTTP] launching GET request to {url}")
response = requests.get(url, headers=headers)
else:
# Validate and convert the payload for other types of requests
if payload_json:
payload = json.loads(payload_json)
else:
payload = {}
print(f"[HTTP] launching {method} request to {url}, payload=\n{json.dumps(payload, indent=2)}")
response = requests.request(method, url, json=payload, headers=headers)

return {"status_code": response.status_code, "headers": dict(response.headers), "body": response.text}
except Exception as e:
return {"error": str(e)}

def pause_heartbeats(self, minutes, max_pause=MAX_PAUSE_HEARTBEATS):
"""Pause timed heartbeats for N minutes"""
minutes = min(max_pause, minutes)
Expand Down
2 changes: 1 addition & 1 deletion memgpt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async def main(

memgpt_agent = presets.use_preset(
# presets.DEFAULT,
"memgpt_files",
"memgpt_extras",
cfg.model,
personas.get_persona_text(*chosen_persona),
humans.get_human_text(*chosen_human),
Expand Down
4 changes: 3 additions & 1 deletion memgpt/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def use_preset(preset_name, model, persona, human, interface, persistence_manage
first_message_verify_mono=True if "gpt-4" in model else False,
)

elif preset_name == "memgpt_files":
elif preset_name == "memgpt_extras":
functions = [
"send_message",
"pause_heartbeats",
Expand All @@ -54,6 +54,8 @@ def use_preset(preset_name, model, persona, human, interface, persistence_manage
# extra for read/write to files
"read_from_text_file",
"append_to_text_file",
# internet access
"http_request",
]
available_functions = [v for k, v in gpt_functions.FUNCTIONS_CHAINING.items() if k in functions]
printd(f"Available functions:\n", [x["name"] for x in available_functions])
Expand Down
22 changes: 13 additions & 9 deletions memgpt/prompts/gpt_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,26 +283,30 @@
"required": ["filename", "content", "request_heartbeat"],
},
},
"archival_memory_search": {
"name": "archival_memory_search",
"description": "Search archival memory using semantic (embedding-based) search.",
"http_request": {
"name": "http_request",
"description": "Generates an HTTP request and returns the response.",
"parameters": {
"type": "object",
"properties": {
"query": {
"method": {
"type": "string",
"description": "String to search for.",
"description": "The HTTP method (e.g., 'GET', 'POST').",
},
"page": {
"type": "integer",
"description": "Allows you to page through results. Only use on a follow-up query. Defaults to 0 (first page).",
"url": {
"type": "string",
"description": "The URL for the request",
},
"payload": {
"type": "string",
"description": "A JSON string representing the request payload.",
},
"request_heartbeat": {
"type": "boolean",
"description": FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
"required": ["name", "query", "page", "request_heartbeat"],
"required": ["method", "url", "request_heartbeat"],
},
},
}

0 comments on commit e153311

Please sign in to comment.