GET
/
api
/
v1
/
users
/
context
/
{user_id}
Python
# To use the Python SDK, install the package:
# pip install memobase

from memobase import MemoBaseClient

client = MemoBaseClient(project_url='PROJECT_URL', api_key='PROJECT_TOKEN')

context = u.context()
{
  "data": {
    "context": "<string>"
  },
  "errno": 0,
  "errmsg": ""
}

Return a string of the user’s personalized context you can directly insert it into your prompt.

Format:

<memory>
# Below is the user profile:
{profile}

# Below is the latest events of the user:
{event}
</memory>
Please provide your answer using the information within the <memory> tag at the appropriate time.

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Path Parameters

user_id
string
required

The ID of the user

Query Parameters

max_token_size
integer
default:1000

Max token size of returned Context

prefer_topics
string[]

Rank prefer topics at first to try to keep them in filtering, default order is by updated time

only_topics
string[]

Only return profiles with these topics, default is all

max_subtopic_size
integer

Max subtopic size of the same topic in returned Context

topic_limits_json
string

Set specific subtopic limits for topics in JSON, for example {"topic1": 3, "topic2": 5}. The limits in this param will override max_subtopic_size.

profile_event_ratio
number
default:0.6

Profile event ratio of returned Context

require_event_summary
boolean
default:false

Whether to require event summary in returned Context

chats_str
string

Pass the recent chats to enable context search. Memobase will use those chats to search for relevant events. It's a list of chats in OpenAI Message format, for example: [{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi"}].

NOTICE

  • It will increase your latency by 0.1-1 seconds, because Memobase will use Embedding to search for relevant profiles and events.
  • It will cost your Memobase tokens, roughly 100~200 tokens per chat based on the profile size.
  • The profiles in the context will not be searched by the chats_str.
  • If you want also search profiles, see full_profile_and_only_search_event query parameter.
event_similarity_threshold
number
default:0.2

Event similarity threshold of returned Context

time_range_in_days
integer
default:180

Only allow events within the past few days, default is 180

customize_context_prompt
string

Customize context prompt template.

  • use {profile_section} to refer to the profile section
  • use {event_section} to refer to the event section

For example:

# Memory
Unless the user has relevant queries, do not actively mention those memories in the conversation.
## User Background:
{profile_section}

## Latest Events:
{event_section}
full_profile_and_only_search_event
boolean
default:true

If you pass chats_str and set this to False, Memobase will search for relevant profiles and events at the same time. NOTICE

  • It will increase your latency by 2-5(based on the profile size) seconds, because Memobase will use LLM and Embedding to search for relevant profiles and events.
  • It will cost your Memobase tokens, roughly 100~1000 tokens per chat based on the profile size.
fill_window_with_events
boolean
default:false

If set to True, Memobase will fill the token window with the rest events.

Response

200
application/json

Successful Response

The response is of type object.