Trubrics는 AI 모델에 대한 사용자 프롬프트와 피드백을 수집, 분석 및 관리할 수 있는 LLM 사용자 분석 플랫폼입니다. Trubrics에 대한 자세한 내용은 Trubrics repo를 확인하세요.
이 가이드에서는 TrubricsCallbackHandler를 설정하는 방법을 다룹니다.

설치 및 설정

pip install -qU  trubrics langchain langchain-community

Trubrics 자격 증명 얻기

Trubrics 계정이 없다면 여기에서 계정을 생성하세요. 이 튜토리얼에서는 계정 생성 시 자동으로 만들어지는 default 프로젝트를 사용합니다. 이제 자격 증명을 환경 변수로 설정하세요:
import os

os.environ["TRUBRICS_EMAIL"] = "***@***"
os.environ["TRUBRICS_PASSWORD"] = "***"
from langchain_community.callbacks.trubrics_callback import TrubricsCallbackHandler

사용법

TrubricsCallbackHandler는 다양한 선택적 인수를 받을 수 있습니다. Trubrics 프롬프트에 전달할 수 있는 kwargs는 여기를 참조하세요.
class TrubricsCallbackHandler(BaseCallbackHandler):

    """
    Callback handler for Trubrics.

    Args:
        project: a trubrics project, default project is "default"
        email: a trubrics account email, can equally be set in env variables
        password: a trubrics account password, can equally be set in env variables
        **kwargs: all other kwargs are parsed and set to trubrics prompt variables, or added to the `metadata` dict
    """

예제

다음은 LangChain LLM 또는 Chat Model과 함께 TrubricsCallbackHandler를 사용하는 두 가지 예제입니다. OpenAI 모델을 사용할 것이므로 여기에서 OPENAI_API_KEY 키를 설정하세요:
os.environ["OPENAI_API_KEY"] = "sk-***"

1. LLM 사용

from langchain_openai import OpenAI
llm = OpenAI(callbacks=[TrubricsCallbackHandler()])
2023-09-26 11:30:02.149 | INFO     | trubrics.platform.auth:get_trubrics_auth_token:61 - User [email protected] has been authenticated.
res = llm.generate(["Tell me a joke", "Write me a poem"])
2023-09-26 11:30:07.760 | INFO     | trubrics.platform:log_prompt:102 - User prompt saved to Trubrics.
2023-09-26 11:30:08.042 | INFO     | trubrics.platform:log_prompt:102 - User prompt saved to Trubrics.
print("--> GPT's joke: ", res.generations[0][0].text)
print()
print("--> GPT's poem: ", res.generations[1][0].text)
--> GPT's joke:

Q: What did the fish say when it hit the wall?
A: Dam!

--> GPT's poem:

A Poem of Reflection

I stand here in the night,
The stars above me filling my sight.
I feel such a deep connection,
To the world and all its perfection.

A moment of clarity,
The calmness in the air so serene.
My mind is filled with peace,
And I am released.

The past and the present,
My thoughts create a pleasant sentiment.
My heart is full of joy,
My soul soars like a toy.

I reflect on my life,
And the choices I have made.
My struggles and my strife,
The lessons I have paid.

The future is a mystery,
But I am ready to take the leap.
I am ready to take the lead,
And to create my own destiny.

2. Chat Model 사용

from langchain.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
chat_llm = ChatOpenAI(
    callbacks=[
        TrubricsCallbackHandler(
            project="default",
            tags=["chat model"],
            user_id="user-id-1234",
            some_metadata={"hello": [1, 2]},
        )
    ]
)
chat_res = chat_llm.invoke(
    [
        SystemMessage(content="Every answer of yours must be about OpenAI."),
        HumanMessage(content="Tell me a joke"),
    ]
)
2023-09-26 11:30:10.550 | INFO     | trubrics.platform:log_prompt:102 - User prompt saved to Trubrics.
print(chat_res.content)
Why did the OpenAI computer go to the party?

Because it wanted to meet its AI friends and have a byte of fun!

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