LangChain integration for PrefID - Preference-aware AI agents
Project description
langchain-prefid
Official LangChain integration for PrefID - Build preference-aware AI agents that adapt to user preferences
What is PrefID?
PrefID is identity-aware AI memory infrastructure that helps AI agents understand:
- WHAT users like (food, music, travel preferences)
- HOW users want responses (thinking preferences - brevity, detail, structure)
Installation
pip install langchain-prefid
Quick Start
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_prefid import create_prefid_tools
# Create PrefID tools
tools = create_prefid_tools(
client_id="your-prefid-client-id",
access_token="user-access-token",
user_id="user_123"
)
# Create agent
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant. Use the preference tools to personalize responses."),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
# Use it!
result = executor.invoke({
"input": "Recommend a restaurant for tonight"
})
Available Tools
1. get_user_preferences
Get user preferences for any domain (food, music, travel, coding, etc.)
# Agent will call this tool
"Get user's food preferences to recommend restaurants"
2. get_thinking_preferences
Get HOW the user wants AI to respond (Atom of Thought)
# Returns:
# - Reasoning style (stepwise, summary_first)
# - Decision style (recommend, tradeoffs, options)
# - Verbosity (concise, detailed, examples)
# - Autonomy (proactive, confirm_first)
3. learn_thinking_preference
Learn and remember how the user wants responses
# When user says: "I prefer brief answers"
# Agent calls this tool to remember it
4. explain_response_style
Introspection - explain why AI is responding a certain way
# When user asks: "Why are you responding like this?"
# Agent explains active preferences
Example Agent
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_prefid import create_prefid_tools
# Setup
tools = create_prefid_tools(
client_id="...",
access_token="...",
user_id="user_123"
)
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
system_prompt = """You are a restaurant recommendation assistant.
BEFORE recommending:
1. Call get_thinking_preferences() to learn HOW to respond
2. Call get_user_preferences('food_profile') to learn WHAT they like
3. Structure response according to their preferences
If user asks why you responded that way, call explain_response_style().
If user tells you how they prefer responses, call learn_thinking_preference().
"""
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Run
response = executor.invoke({
"input": "Where should I eat tonight?"
})
Individual Tools
You can also use tools individually:
from langchain_prefid import (
PrefIDPreferenceTool,
PrefIDThinkingTool,
PrefIDLearnTool,
PrefIDWhyTool
)
from prefid import PrefID
prefid = PrefID(client_id="...", access_token="...")
# Create individual tools
pref_tool = PrefIDPreferenceTool(prefid_client=prefid)
thinking_tool = PrefIDThinkingTool(prefid_client=prefid)
learn_tool = PrefIDLearnTool(prefid_client=prefid)
why_tool = PrefIDWhyTool(prefid_client=prefid)
# Use them
food_prefs = pref_tool.invoke({"domain": "food_profile"})
thinking = thinking_tool.invoke({"user_id": "user_123"})
Key Concepts
Atom of Thought (AoT)
PrefID's unique feature - learns HOW users want AI to respond:
- "I prefer step-by-step explanations" → Reasoning: stepwise
- "Just give me one recommendation" → Decision: recommend
- "Keep it brief" → Verbosity: concise
This goes beyond WHAT users like to HOW they think.
Thinking Preferences vs Content Preferences
- Content: What music/food/travel they like
- Thinking: How they want information structured
Both are important for truly personalized AI.
Examples
See /examples for complete working examples:
restaurant_recommender.py- Full agent with all toolsbasic_agent.py- Simple preference-aware agent
LangSmith Hub
Standard prompts for PrefID agents are available on LangSmith Hub:
| Prompt | Description |
|---|---|
| talentxmdu/restaurant-recommender | Full agent prompt with AoT + Preference logic |
| talentxmdu/basic-agent | Minimal prompt for preference aware agents |
Usage:
from langchain import hub
prompt = hub.pull("talentxmdu/restaurant-recommender")
Get PrefID Credentials
- Sign up at prefid.in
- Create an OAuth client in Dashboard → Connected Apps
- Get your
client_idandclient_secret
Links
- Documentation: https://docs.prefid.in
- PrefID SDK: https://github.com/Talentxmdu/PrefID
- LangChain: https://python.langchain.com/
- Support: hello@prefid.in
License
MIT © PrefID Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_prefid-0.2.0.tar.gz.
File metadata
- Download URL: langchain_prefid-0.2.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
366c9e5de455bd9ff73675a850ed8fabad4d86309d578017fc920bd706e4a925
|
|
| MD5 |
aa536c325fa3a3d6ed307ae992b671fb
|
|
| BLAKE2b-256 |
28c6eaa436b14cf82b88acbedc74ac041aa899bca90baf0f0291ffa9e0e7bd24
|
File details
Details for the file langchain_prefid-0.2.0-py3-none-any.whl.
File metadata
- Download URL: langchain_prefid-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcc490a8f9f6a1285c929757a16b5362d6f2de5c450903f032b6606e57d537a7
|
|
| MD5 |
f8146290e8ab8663a89c424f1a0a060c
|
|
| BLAKE2b-256 |
ba64b435f75262a783dac224f97172e7bb0740bc13564da6d77aabe0fb7d328d
|