Skip to main content

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

PyPI Python 3.9+ License: MIT

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 tools
  • basic_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

  1. Sign up at prefid.in
  2. Create an OAuth client in Dashboard → Connected Apps
  3. Get your client_id and client_secret

Links

License

MIT © PrefID Team

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_prefid-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_prefid-0.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

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

Hashes for langchain_prefid-0.2.0.tar.gz
Algorithm Hash digest
SHA256 366c9e5de455bd9ff73675a850ed8fabad4d86309d578017fc920bd706e4a925
MD5 aa536c325fa3a3d6ed307ae992b671fb
BLAKE2b-256 28c6eaa436b14cf82b88acbedc74ac041aa899bca90baf0f0291ffa9e0e7bd24

See more details on using hashes here.

File details

Details for the file langchain_prefid-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_prefid-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcc490a8f9f6a1285c929757a16b5362d6f2de5c450903f032b6606e57d537a7
MD5 f8146290e8ab8663a89c424f1a0a060c
BLAKE2b-256 ba64b435f75262a783dac224f97172e7bb0740bc13564da6d77aabe0fb7d328d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page