Skip to main content
LangSmith Python 및 TypeScript SDK를 사용하여 프롬프트를 프로그래밍 방식으로 관리할 수 있습니다.
이전에는 이 기능이 langchainhub 패키지에 포함되어 있었으나 현재는 더 이상 사용되지 않습니다. 앞으로 모든 기능은 langsmith 패키지에서 제공됩니다.

패키지 설치

Python에서는 LangSmith SDK를 직접 사용하거나(권장, 전체 기능 제공) LangChain 패키지를 통해 사용할 수 있습니다(프롬프트 푸시 및 풀 기능으로 제한됨). TypeScript에서는 프롬프트를 풀링하기 위해 LangChain npm 패키지를 사용해야 합니다(푸시 기능도 제공). 다른 모든 기능은 LangSmith 패키지를 사용하세요.
pip install -U langsmith # version >= 0.1.99
pip install -U langchain langsmith # langsmith version >= 0.1.99 and langchain >= 0.2.13

환경 변수 설정

LangSmith에서 현재 워크스페이스의 API 키로 LANGSMITH_API_KEY가 이미 설정되어 있다면 이 단계를 건너뛸 수 있습니다. 그렇지 않은 경우, LangSmith에서 Settings > API Keys > Create API Key로 이동하여 워크스페이스용 API 키를 받으세요. 환경 변수를 설정하세요.
export LANGSMITH_API_KEY="lsv2_..."
우리가 “프롬프트(prompts)“라고 부르는 것은 과거에는 “리포지토리(repos)“라고 불렸으므로, 코드에서 “repo”에 대한 모든 참조는 프롬프트를 의미합니다.

프롬프트 푸시

새 프롬프트를 생성하거나 기존 프롬프트를 업데이트하려면 push prompt 메서드를 사용할 수 있습니다.
from langsmith import Client
from langchain_core.prompts import ChatPromptTemplate

client = Client()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
url = client.push_prompt("joke-generator", object=prompt)
# url is a link to the prompt in the UI
print(url)
프롬프트와 모델의 RunnableSequence로 프롬프트를 푸시할 수도 있습니다. 이는 이 프롬프트와 함께 사용하려는 모델 구성을 저장하는 데 유용합니다. 제공자는 LangSmith 플레이그라운드에서 지원되어야 합니다. (지원되는 제공자 설정 확인: Supported Providers)
from langsmith import Client
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

client = Client()
model = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
chain = prompt | model
client.push_prompt("joke-generator-with-model", object=chain)

프롬프트 풀

프롬프트를 풀하려면 pull prompt 메서드를 사용할 수 있으며, 이는 프롬프트를 langchain PromptTemplate로 반환합니다. 비공개 프롬프트를 풀하려면 소유자 핸들을 지정할 필요가 없습니다(물론 핸들이 있다면 지정할 수 있습니다). LangChain Hub에서 공개 프롬프트를 풀하려면 프롬프트 작성자의 핸들을 지정해야 합니다.
from langsmith import Client
from langchain_openai import ChatOpenAI

client = Client()
prompt = client.pull_prompt("joke-generator")
model = ChatOpenAI(model="gpt-4o-mini")
chain = prompt | model
chain.invoke({"topic": "cats"})
프롬프트 푸시와 마찬가지로, 프롬프트와 모델의 RunnableSequence로 프롬프트를 풀할 수도 있습니다. 프롬프트를 풀할 때 include_model을 지정하기만 하면 됩니다. 저장된 프롬프트에 모델이 포함되어 있으면 RunnableSequence로 반환됩니다. 사용 중인 모델에 대한 적절한 환경 변수가 설정되어 있는지 확인하세요.
from langsmith import Client

client = Client()
chain = client.pull_prompt("joke-generator-with-model", include_model=True)
chain.invoke({"topic": "cats"})
프롬프트를 풀할 때 특정 커밋 해시 또는 커밋 태그를 지정하여 프롬프트의 특정 버전을 풀할 수도 있습니다.
prompt = client.pull_prompt("joke-generator:12344e88")
LangChain Hub에서 공개 프롬프트를 풀하려면 프롬프트 작성자의 핸들을 지정해야 합니다.
prompt = client.pull_prompt("efriis/my-first-prompt")
프롬프트를 풀할 때, Node.js 또는 동적 import를 지원하는 환경을 사용하는 경우, langchain/hub/node 엔트리포인트 사용을 권장합니다. 이는 프롬프트 구성과 연결된 모델의 역직렬화를 자동으로 처리하기 때문입니다.Node 환경이 아닌 경우, “includeModel”은 OpenAI가 아닌 모델에서는 지원되지 않으며 기본 langchain/hub 엔트리포인트를 사용해야 합니다.

LangChain 없이 프롬프트 사용하기

LangSmith에 프롬프트를 저장하면서 모델 제공자의 API와 직접 함께 사용하고 싶다면, 변환 메서드를 사용할 수 있습니다. 이 메서드는 프롬프트를 OpenAI 또는 Anthropic API에 필요한 페이로드로 변환합니다. 이러한 변환 메서드는 LangChain 통합 패키지 내의 로직에 의존하며, 선택한 공식 SDK 외에도 적절한 패키지를 종속성으로 설치해야 합니다. 다음은 몇 가지 예제입니다:

OpenAI

pip install -U langchain_openai
from openai import OpenAI
from langsmith.client import Client, convert_prompt_to_openai_format

# langsmith client
client = Client()
# openai client
oai_client = OpenAI()

# pull prompt and invoke to populate the variables
prompt = client.pull_prompt("joke-generator")
prompt_value = prompt.invoke({"topic": "cats"})
openai_payload = convert_prompt_to_openai_format(prompt_value)
openai_response = oai_client.chat.completions.create(**openai_payload)

Anthropic

pip install -U langchain_anthropic
from anthropic import Anthropic
from langsmith.client import Client, convert_prompt_to_anthropic_format

# langsmith client
client = Client()
# anthropic client
anthropic_client = Anthropic()

# pull prompt and invoke to populate the variables
prompt = client.pull_prompt("joke-generator")
prompt_value = prompt.invoke({"topic": "cats"})
anthropic_payload = convert_prompt_to_anthropic_format(prompt_value)
anthropic_response = anthropic_client.messages.create(**anthropic_payload)

프롬프트 목록 조회, 삭제 및 좋아요

list prompts, delete prompt, like promptunlike prompt 메서드를 사용하여 프롬프트를 목록 조회, 삭제 및 좋아요/좋아요 취소할 수도 있습니다. 이러한 메서드에 대한 자세한 설명은 LangSmith SDK client를 참조하세요.
# List all prompts in my workspace
prompts = client.list_prompts()

# List my private prompts that include "joke"
prompts = client.list_prompts(query="joke", is_public=False)

# Delete a prompt
client.delete_prompt("joke-generator")

# Like a prompt
client.like_prompt("efriis/my-first-prompt")

# Unlike a prompt
client.unlike_prompt("efriis/my-first-prompt")

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I