feat: Create RESTClient and Admin client for interacting with server from python (#1033)
This commit is contained in:
@@ -4,72 +4,42 @@ excerpt: Developing using the MemGPT Python client
|
||||
category: 6580dab16cade8003f996d17
|
||||
---
|
||||
|
||||
The fastest way to integrate MemGPT with your own Python projects is through the `MemGPT` [client class](https://github.com/cpacker/MemGPT/blob/main/memgpt/client/client.py):
|
||||
The fastest way to integrate MemGPT with your own Python projects is through the [client class](https://github.com/cpacker/MemGPT/blob/main/memgpt/client/client.py):
|
||||
|
||||
```python
|
||||
from memgpt import MemGPT
|
||||
from memgpt import create_client
|
||||
|
||||
# Create a MemGPT client object (sets up the persistent state)
|
||||
client = MemGPT(
|
||||
quickstart="openai",
|
||||
config={
|
||||
"openai_api_key": "YOUR_API_KEY"
|
||||
}
|
||||
# Connect to the server as a user
|
||||
client = create_client()
|
||||
|
||||
# Create an agent
|
||||
agent_info = client.create_agent(
|
||||
name="my_agent",
|
||||
persona="You are a friendly agent.",
|
||||
human="Bob is a friendly human."
|
||||
)
|
||||
|
||||
# You can set many more parameters, this is just a basic example
|
||||
agent_state = client.create_agent(
|
||||
agent_config={
|
||||
"persona": "sam_pov",
|
||||
"human": "cs_phd",
|
||||
}
|
||||
)
|
||||
|
||||
# Now that we have an agent_name identifier, we can send it a message!
|
||||
# The response will have data from the MemGPT agent
|
||||
my_message = "Hi MemGPT! How's it going?"
|
||||
response = client.user_message(agent_id=agent_state.id, message=my_message)
|
||||
# Send a message to the agent
|
||||
messages = client.user_message(agent_id=agent_info.id, message="Hello, agent!")
|
||||
```
|
||||
|
||||
## More in-depth example of using the MemGPT Python client
|
||||
|
||||
```python
|
||||
from memgpt.config import AgentConfig
|
||||
from memgpt import MemGPT
|
||||
from memgpt import constants
|
||||
from memgpt.cli.cli import QuickstartChoice
|
||||
from memgpt import create_client
|
||||
|
||||
# Connect to the server as a user
|
||||
client = create_client()
|
||||
|
||||
client = MemGPT(
|
||||
# When auto_save is 'True' then the agent(s) will be saved after every
|
||||
# user message. This may have performance implications, so you
|
||||
# can otherwise choose when to save explicitly using client.save().
|
||||
auto_save=True,
|
||||
|
||||
# Quickstart will automatically configure MemGPT (without having to run `memgpt configure`
|
||||
# If you choose 'openai' then you must set the api key (env or in config)
|
||||
quickstart=QuickstartChoice.memgpt_hosted,
|
||||
|
||||
# Allows you to override default config generated by quickstart or `memgpt configure`
|
||||
config={}
|
||||
# Create an agent
|
||||
agent_info = client.create_agent(
|
||||
name="my_agent",
|
||||
persona="You are a friendly agent.",
|
||||
human="Bob is a friendly human."
|
||||
)
|
||||
|
||||
# Create an AgentConfig with default persona and human txt
|
||||
# In this case, assume we wrote a custom persona file "my_persona.txt", located at ~/.memgpt/personas/my_persona.txt
|
||||
# Same for a custom user file "my_user.txt", located at ~/.memgpt/humans/my_user.txt
|
||||
agent_config = AgentConfig(
|
||||
name="CustomAgent",
|
||||
persona="my_persona",
|
||||
human="my_user",
|
||||
preset="memgpt_chat",
|
||||
model="gpt-4",
|
||||
)
|
||||
|
||||
# Create the agent according to AgentConfig we set up. If an agent with
|
||||
# the same name already exists it will simply return, unless you set
|
||||
# throw_if_exists to 'True'
|
||||
agent_state = client.create_agent(agent_config=agent_config)
|
||||
|
||||
# Send a message to the agent
|
||||
messages = client.user_message(agent_id=agent_info.id, message="Hello, agent!")
|
||||
# Create a helper that sends a message and prints the assistant response only
|
||||
def send_message(message: str):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user