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 31, 2023
1 parent 3bb9efc commit 6820765
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
34 changes: 34 additions & 0 deletions memgpt/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pickle
import math
import os
import requests
import json
import threading

Expand Down Expand Up @@ -258,6 +259,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 @@ -834,6 +836,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
4 changes: 2 additions & 2 deletions memgpt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,15 @@ async def run_agent_loop(memgpt_agent, first, no_verify=False, cfg=None, legacy=
continue

elif user_input.lower() == "/dump":
await print_messages(memgpt_agent.messages)
await memgpt.interface.print_messages(memgpt_agent.messages)
continue

elif user_input.lower() == "/dumpraw":
await memgpt.interface.print_messages_raw(memgpt_agent.messages)
continue

elif user_input.lower() == "/dump1":
await print_messages(memgpt_agent.messages[-1])
await memgpt.interface.print_messages(memgpt_agent.messages[-1])
continue

elif user_input.lower() == "/memory":
Expand Down
2 changes: 1 addition & 1 deletion memgpt/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def use_preset(preset_name, agent_config, model, persona, human, interface, pers
first_message_verify_mono=True if "gpt-4" in model else False,
)

if preset_name == "memgpt_chat_sync": # TODO: remove me after we move the CLI to AgentSync
elif preset_name == "memgpt_chat_sync": # TODO: remove me after we move the CLI to AgentSync
functions = [
"send_message",
"pause_heartbeats",
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 6820765

Please sign in to comment.