Skip to main content
많은 LLM 애플리케이션은 사용자와 LLM 애플리케이션이 여러 차례에 걸쳐 대화를 주고받는 챗봇과 유사한 인터페이스를 제공합니다. 이러한 대화를 추적하기 위해 LangSmith의 스레드 기능을 사용할 수 있습니다.

트레이스를 스레드로 그룹화하기

스레드는 단일 대화를 나타내는 일련의 트레이스입니다. 각 응답은 자체 트레이스로 표시되지만, 이러한 트레이스들은 동일한 스레드의 일부로 함께 연결됩니다. 트레이스를 함께 연결하려면 해당 스레드의 고유 식별자를 값으로 하는 특수한 metadata 키를 전달해야 합니다. 키 이름은 다음 중 하나여야 합니다:
  • session_id
  • thread_id
  • conversation_id
값은 원하는 모든 문자열이 될 수 있지만, f47ac10b-58cc-4372-a567-0e02b2c3d479와 같은 UUID를 사용하는 것을 권장합니다. 트레이스에 메타데이터를 추가하는 방법에 대한 지침은 이 가이드를 참조하세요.

예제

이 예제는 장기 실행 채팅을 유지하기 위해 구조화된 메시지 형식을 사용하여 대화 기록을 로깅하고 검색하는 방법을 보여줍니다.
import os
from typing import List, Dict, Any, Optional

import openai
from langsmith import traceable, Client
import langsmith as ls
from langsmith.wrappers import wrap_openai

# Initialize clients
client = wrap_openai(openai.Client())
langsmith_client = Client()

# Configuration
LANGSMITH_PROJECT = "project-with-threads"
THREAD_ID = "thread-id-1"
langsmith_extra={"project_name": LANGSMITH_PROJECT, "metadata":{"session_id": THREAD_ID}}

# gets a history of all LLM calls in the thread to construct conversation history
def get_thread_history(thread_id: str, project_name: str):
    # Filter runs by the specific thread and project
    filter_string = f'and(in(metadata_key, ["session_id","conversation_id","thread_id"]), eq(metadata_value, "{thread_id}"))'
    # Only grab the LLM runs
    runs = [r for r in langsmith_client.list_runs(project_name=project_name, filter=filter_string, run_type="llm")]

    # Sort by start time to get the most recent interaction
    runs = sorted(runs, key=lambda run: run.start_time, reverse=True)

    # Reconstruct the conversation state
    latest_run = runs[0]
    return latest_run.inputs['messages'] + [latest_run.outputs['choices'][0]['message']]


@traceable(name="Chat Bot")
def chat_pipeline(messages: list, get_chat_history: bool = False):
    # Whether to continue an existing thread or start a new one
    if get_chat_history:
        run_tree = ls.get_current_run_tree()
        # Get existing conversation history and append new messages
        history_messages = get_thread_history(run_tree.extra["metadata"]["session_id"], run_tree.session_name)
        all_messages = history_messages + messages
        # Include the complete conversation in the input for tracing
        input_messages = all_messages
    else:
        all_messages = messages
        input_messages = messages

    # Invoke the model
    chat_completion = client.chat.completions.create(
        model="gpt-4o-mini", messages=all_messages
    )

    # Return the complete conversation including input and response
    response_message = chat_completion.choices[0].message
    return {
        "messages": input_messages + [response_message]
    }

# Format message
messages = [
    {
        "content": "Hi, my name is Sally",
        "role": "user"
    }
]
get_chat_history = False

# Call the chat pipeline
result = chat_pipeline(messages, get_chat_history, langsmith_extra=langsmith_extra)
몇 초 정도 기다린 후, 다음 호출을 통해 대화를 이어갈 수 있습니다. get_chat_history=True/getChatHistory: true를 전달하면 중단된 지점에서 대화를 이어갈 수 있습니다. 이는 LLM이 최신 메시지에만 응답하는 대신 전체 메시지 기록을 받아 응답한다는 것을 의미합니다.
# Continue the conversation.
messages = [
    {
        "content": "What is my name?",
        "role": "user"
    }
]
get_chat_history = True

chat_pipeline(messages, get_chat_history, langsmith_extra=langsmith_extra)
대화를 계속 이어가세요. 과거 메시지가 포함되어 있으므로 LLM이 대화를 기억합니다.
# Continue the conversation.
messages = [
    {
        "content": "What was the first message I sent you?",
        "role": "user"
    }
]
get_chat_history = True

chat_pipeline(messages, get_chat_history, langsmith_extra=langsmith_extra)

스레드 보기

프로젝트 상세 페이지의 스레드 탭을 클릭하여 스레드를 볼 수 있습니다. 그러면 가장 최근 활동 순으로 정렬된 모든 스레드 목록이 표시됩니다.
스레드 테이블을 보여주는 LangSmith UI

스레드 보기

특정 스레드를 클릭할 수 있습니다. 그러면 해당 스레드의 기록이 열립니다.
스레드 테이블을 보여주는 LangSmith UI
스레드는 두 가지 방식으로 볼 수 있습니다: 페이지 상단의 버튼을 사용하여 두 보기 간에 전환하거나 키보드 단축키 T를 사용하여 두 보기를 토글할 수 있습니다.

스레드 개요

스레드 개요 페이지는 대화의 각 턴에 대한 입력과 출력을 볼 수 있는 챗봇과 유사한 UI를 표시합니다. 구성 버튼을 클릭하여 개요에 표시되는 입력 및 출력 필드를 구성하거나 여러 필드를 표시할 수 있습니다. 입력 및 출력에 대한 JSON 경로는 음수 인덱싱을 지원하므로 -1을 사용하여 배열의 마지막 요소에 접근할 수 있습니다. 예를 들어, inputs.messages[-1].contentmessages 배열의 마지막 메시지에 접근합니다.

트레이스 보기

여기의 트레이스 보기는 단일 실행을 볼 때의 트레이스 보기와 유사하지만, 스레드의 각 턴에 대한 모든 실행에 쉽게 접근할 수 있다는 점이 다릅니다.

피드백 보기

스레드를 볼 때 페이지 상단에 피드백이라는 섹션이 표시됩니다. 여기에서 스레드를 구성하는 각 실행에 대한 피드백을 볼 수 있습니다. 이 피드백은 집계되므로 동일한 기준으로 스레드의 각 실행을 평가하면 모든 실행에 걸친 평균 점수가 표시됩니다. 여기에 남겨진 스레드 수준 피드백도 확인할 수 있습니다.

스레드 수준 필터 저장

프로젝트 수준에서 필터를 저장하는 것과 유사하게, 스레드 수준에서도 자주 사용하는 필터를 저장할 수 있습니다. 스레드 테이블에서 필터를 저장하려면 필터 버튼을 사용하여 필터를 설정한 다음 필터 저장 버튼을 클릭하세요. 주석 달기트레이스 열기를 각각 클릭하여 사이드 패널에서 트레이스를 열거나 주석을 달 수 있습니다.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I