Skip to main content

Argilla

Argilla - 用于LLM的开源数据平台

Argilla是一个用于LLM的开源数据整理平台。 使用Argilla,每个人都可以通过更快的数据整理使用人工和机器反馈来构建强大的语言模型。我们为MLOps周期中的每个步骤提供支持,从数据标注到模型监控。

在Colab中打开

在本指南中,我们将演示如何跟踪您的LLM的输入和响应,以在Argilla中生成数据集,使用ArgillaCallbackHandler

跟踪LLM的输入和输出对于生成未来微调数据集非常有用。当您使用LLM为特定任务生成数据时,例如问题回答、摘要或翻译时,这尤其有用。

安装和设置

pip install argilla --upgrade
pip install openai

获取API凭据

要获取Argilla API凭据,请按照以下步骤操作:

  1. 进入Argilla UI。
  2. 点击您的个人资料图片,进入“我的设置”。
  3. 然后复制API密钥。

在Argilla中,API URL将与您的Argilla UI的URL相同。

要获取OpenAI API凭据,请访问 https://platform.openai.com/account/api-keys

import os

os.environ["ARGILLA_API_URL"] = "..."
os.environ["ARGILLA_API_KEY"] = "..."

os.environ["OPENAI_API_KEY"] = "..."

设置Argilla

要使用ArgillaCallbackHandler,我们需要在Argilla中创建一个新的FeedbackDataset,以跟踪您的LLM实验。请使用以下代码进行操作:

import argilla as rg
from packaging.version import parse as parse_version

if parse_version(rg.__version__) < parse_version("1.8.0"):
raise RuntimeError(
"`FeedbackDataset`仅在Argilla v1.8.0或更高版本中可用,请升级`argilla`,如`pip install argilla --upgrade`。"
)
dataset = rg.FeedbackDataset(
fields=[
rg.TextField(name="prompt"),
rg.TextField(name="response"),
],
questions=[
rg.RatingQuestion(
name="response-rating",
description="您如何评价响应的质量?",
values=[1, 2, 3, 4, 5],
required=True,
),
rg.TextQuestion(
name="response-feedback",
description="对响应有何反馈意见?",
required=False,
),
],
guidelines="您被要求评价响应的质量并提供反馈意见。",
)

rg.init(
api_url=os.environ["ARGILLA_API_URL"],
api_key=os.environ["ARGILLA_API_KEY"],
)

dataset.push_to_argilla("langchain-dataset")

📌 注意:目前,FeedbackDataset.fields仅支持提示-响应对,因此ArgillaCallbackHandler将仅跟踪提示,即LLM输入,和响应,即LLM输出。

跟踪

要使用ArgillaCallbackHandler,您可以使用以下代码,或者只需复制以下部分中介绍的示例之一。

from langchain.callbacks import ArgillaCallbackHandler

argilla_callback = ArgillaCallbackHandler(
dataset_name="langchain-dataset",
api_url=os.environ["ARGILLA_API_URL"],
api_key=os.environ["ARGILLA_API_KEY"],
)

场景1:跟踪LLM

首先,让我们运行一个LLM多次,并在Argilla中捕获生成的提示-响应对。

from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler
from langchain.llms import OpenAI

argilla_callback = ArgillaCallbackHandler(
dataset_name="langchain-dataset",
api_url=os.environ["ARGILLA_API_URL"],
api_key=os.environ["ARGILLA_API_KEY"],
)
callbacks = [StdOutCallbackHandler(), argilla_callback]

llm = OpenAI(temperature=0.9, callbacks=callbacks)
llm.generate(["给我讲个笑话", "给我讲个诗歌"] * 3)
    LLMResult(generations=[[Generation(text='\n\nQ: What did the fish say when he hit the wall? \nA: Dam.', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\n\nThe Moon \n\nThe moon is high in the midnight sky,\nSparkling like a star above.\nThe night so peaceful, so serene,\nFilling up the air with love.\n\nEver changing and renewing,\nA never-ending light of grace.\nThe moon remains a constant view,\nA reminder of life’s gentle pace.\n\nThrough time and space it guides us on,\nA never-fading beacon of hope.\nThe moon shines down on us all,\nAs it continues to rise and elope.', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\n\nQ. What did one magnet say to the other magnet?\nA. "I find you very attractive!"', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text="\n\nThe world is charged with the grandeur of God.\nIt will flame out, like shining from shook foil;\nIt gathers to a greatness, like the ooze of oil\nCrushed. Why do men then now not reck his rod?\n\nGenerations have trod, have trod, have trod;\nAnd all is seared with trade; bleared, smeared with toil;\nAnd wears man's smudge and shares man's smell: the soil\nIs bare now, nor can foot feel, being shod.\n\nAnd for all this, nature is never spent;\nThere lives the dearest freshness deep down things;\nAnd though the last lights off the black West went\nOh, morning, at the brown brink eastward, springs —\n\nBecause the Holy Ghost over the bent\nWorld broods with warm breast and with ah! bright wings.\n\n~Gerard Manley Hopkins", generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\n\nQ: What did one ocean say to the other ocean?\nA: Nothing, they just waved.', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text="\n\nA poem for you\n\nOn a field of green\n\nThe sky so blue\n\nA gentle breeze, the sun above\n\nA beautiful world, for us to love\n\nLife is a journey, full of surprise\n\nFull of joy and full of surprise\n\nBe brave and take small steps\n\nThe future will be revealed with depth\n\nIn the morning, when dawn arrives\n\nA fresh start, no reason to hide\n\nSomewhere down the road, there's a heart that beats\n\nBelieve in yourself, you'll always succeed.", generation_info={'finish_reason': 'stop', 'logprobs': None})]], llm_output={'token_usage': {'completion_tokens': 504, 'total_tokens': 528, 'prompt_tokens': 24}, 'model_name': 'text-davinci-003'})

Argilla UI with LangChain LLM input-response

场景2:在链中跟踪LLM

然后,我们可以使用提示模板创建一个链,然后在Argilla中跟踪初始提示和最终响应。

from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

argilla_callback = ArgillaCallbackHandler(
dataset_name="langchain-dataset",
api_url=os.environ["ARGILLA_API_URL"],
api_key=os.environ["ARGILLA_API_KEY"],
)
callbacks = [StdOutCallbackHandler(), argilla_callback]
llm = OpenAI(temperature=0.9, callbacks=callbacks)

template = """您是一位剧作家。根据剧名,您的任务是为该剧写一个简介。
剧名:{title}
剧作家:这是上述剧的简介:"""
prompt_template = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, callbacks=callbacks)

test_prompts = [{"title": "关于巴黎的大脚怪的纪录片"}]
synopsis_chain.apply(test_prompts)
    

> 进入新的LLMChain链...
格式化后的提示:
您是一位剧作家。根据剧名,您的任务是为该剧写一个简介。
剧名:关于巴黎的大脚怪的纪录片
剧作家:这是上述剧的简介:

> 完成链。




[{'text': "\n\n关于巴黎的大脚怪的纪录片聚焦于一位纪录片制片人及其在巴黎市寻找传说中的大脚怪证据的故事。该剧跟随制片人探索巴黎市,结识各行各业与神秘生物有过邂逅的人们。通过他们的对话,制片人揭开了关于大脚怪的故事,并了解到了该生物在巴黎市的存在真相。随着故事的发展,制片人越来越多地了解了这个神秘生物,以及城市居民的不同观点,以及他们对这个生物的看法。最后,制片人的发现引导他们得出了一些令人惊讶和温馨的结论,关于这个生物的存在以及它在巴黎市居民生活中的重要性。"}]

Argilla UI with LangChain Chain input-response

场景3:使用带有工具的代理

最后,作为更高级的工作流程,您可以创建一个使用一些工具的代理。因此,ArgillaCallbackHandler将跟踪输入和输出,但不会跟踪中间步骤/思考过程,因此给定一个提示,我们记录原始提示和给定提示的最终响应。

请注意,对于此场景,我们将使用Google搜索API(Serp API),因此您需要安装google-search-results,如pip install google-search-results,并将Serp API密钥设置为os.environ["SERPAPI_API_KEY"] = "..."(您可以在 https://serpapi.com/dashboard 找到它),否则下面的示例将无法正常工作。

from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler
from langchain.llms import OpenAI

argilla_callback = ArgillaCallbackHandler(
dataset_name="langchain-dataset",
api_url=os.environ["ARGILLA_API_URL"],
api_key=os.environ["ARGILLA_API_KEY"],
)
callbacks = [StdOutCallbackHandler(), argilla_callback]
llm = OpenAI(temperature=0.9, callbacks=callbacks)

tools = load_tools(["serpapi"], llm=llm, callbacks=callbacks)
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
callbacks=callbacks,
)
agent.run("美国的第一任总统是谁?")
    

> 进入新的AgentExecutor链...
我需要回答一个历史问题
动作:搜索
动作输入:"美国的第一任总统是谁"
观察结果:乔治·华盛顿
思考:乔治·华盛顿是第一任总统
最终答案:乔治·华盛顿是美国的第一任总统。

> 完成链。




'乔治·华盛顿是美国的第一任总统。'

Argilla UI with LangChain Agent input-response