Pull/push a prompt
Install packages
- pip
- yarn
- npm
pip install -U langchain langchainhub langchain-openai
yarn add langchain
npm install -S langchain
Configure environment variables
If you already have LANGCHAIN_API_KEY
set to a personal organization's api key from LangSmith, you can skip this step.
Otherwise, get an API key for your Personal organization by navigating to Settings > API Keys > Create API Key
in LangSmith. (The hub will not work with your non-personal organization's api key!)
Set your environment variable.
export LANGCHAIN_HUB_API_KEY="ls_..."
Pull a prompt and use it
You can pull your own prompts and all of the public prompts in the LangChain Hub.
- Python
- TypeScript
from langchain import hub
# pull a chat prompt
prompt = hub.pull("efriis/my-first-prompt")
# create a model to use it with
from langchain_openai import ChatOpenAI
model = ChatOpenAI()
# use it in a runnable
runnable = prompt | model
response = runnable.invoke({
"profession": "biologist",
"question": "What is special about parrots?",
})
print(response)
// import
import * as hub from "langchain/hub";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
// pull a chat prompt
const prompt = await hub.pull<ChatPromptTemplate>("efriis/my-first-prompt");
// create a model to use it with
const model = new ChatOpenAI();
// use it in a runnable
const runnable = prompt.pipe(model);
const result = await runnable.invoke({
"profession": "biologist",
"question": "What is special about parrots?",
});
console.log(result);
You can also pull a specific commit of a prompt by specifying the commit hash.
prompt = hub.pull("efriis/my-first-prompt:56489e79")
Push a prompt to your personal organization
For this step, you'll need the handle
for your account!
- Python
- TypeScript
from langchain import hub
from langchain.prompts.chat import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
hub.push("<handle>/topic-joke-generator", prompt, new_repo_is_public=False)
import * as hub from "langchain/hub";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
} from '@langchain/core/prompts';
const message = HumanMessagePromptTemplate.fromTemplate(
'tell me a joke about {topic}'
);
const prompt = ChatPromptTemplate.fromMessages([message]);
await hub.push("<handle>/my-first-prompt", prompt, { newRepoIsPublic: false });