Langsmith 无缝支持¶
很多人误以为 LangChain 的 LangSmith 只能与 LangChain 的模型兼容。实际上,LangSmith 是一个用于开发、协作、测试、部署和监控 LLM 应用的统一 DevOps 平台。在本篇博客中,我们将探讨如何使用 LangSmith 与 instructor
一起增强 OpenAI 客户端。
首先,安装必要的包
LangSmith¶
为了使用 langsmith,首先需要设置 LangSmith API 密钥。
接下来,你需要安装 LangSmith SDK
在此示例中,我们将使用 wrap_openai
函数用 LangSmith 包装 OpenAI 客户端。这将允许我们使用 LangSmith 的可观测性和监控功能来监控 OpenAI 客户端。然后我们将使用 instructor
通过 TOOLS
模式修补客户端。这将允许我们使用 instructor
为客户端添加额外功能。
import instructor
import asyncio
from langsmith import traceable
from langsmith.wrappers import wrap_openai
from openai import AsyncOpenAI
from pydantic import BaseModel, Field, field_validator
from typing import List
from enum import Enum
# Wrap the OpenAI client with LangSmith
client = wrap_openai(AsyncOpenAI())
# Patch the client with instructor
client = instructor.from_openai(client)
# Rate limit the number of requests
sem = asyncio.Semaphore(5)
# Use an Enum to define the types of questions
class QuestionType(Enum):
CONTACT = "CONTACT"
TIMELINE_QUERY = "TIMELINE_QUERY"
DOCUMENT_SEARCH = "DOCUMENT_SEARCH"
COMPARE_CONTRAST = "COMPARE_CONTRAST"
EMAIL = "EMAIL"
PHOTOS = "PHOTOS"
SUMMARY = "SUMMARY"
# You can add more instructions and examples in the description
# or you can put it in the prompt in `messages=[...]`
class QuestionClassification(BaseModel):
"""
Predict the type of question that is being asked.
Here are some tips on how to predict the question type:
CONTACT: Searches for some contact information.
TIMELINE_QUERY: "When did something happen?
DOCUMENT_SEARCH: "Find me a document"
COMPARE_CONTRAST: "Compare and contrast two things"
EMAIL: "Find me an email, search for an email"
PHOTOS: "Find me a photo, search for a photo"
SUMMARY: "Summarize a large amount of data"
"""
# If you want only one classification, just change it to
# `classification: QuestionType` rather than `classifications: List[QuestionType]``
chain_of_thought: str = Field(
..., description="The chain of thought that led to the classification"
)
classification: List[QuestionType] = Field(
description=f"An accuracy and correct prediction predicted class of question. Only allowed types: {[t.value for t in QuestionType]}, should be used",
)
@field_validator("classification", mode="before")
def validate_classification(cls, v):
# sometimes the API returns a single value, just make sure it's a list
if not isinstance(v, list):
v = [v]
return v
@traceable(name="classify-question")
async def classify(data: str) -> QuestionClassification:
"""
Perform multi-label classification on the input text.
Change the prompt to fit your use case.
Args:
data (str): The input text to classify.
"""
async with sem: # some simple rate limiting
return data, await client.chat.completions.create(
model="gpt-4-turbo-preview",
response_model=QuestionClassification,
max_retries=2,
messages=[
{
"role": "user",
"content": f"Classify the following question: {data}",
},
],
)
async def main(questions: List[str]):
tasks = [classify(question) for question in questions]
for task in asyncio.as_completed(tasks):
question, label = await task
resp = {
"question": question,
"classification": [c.value for c in label.classification],
"chain_of_thought": label.chain_of_thought,
}
resps.append(resp)
return resps
if __name__ == "__main__":
import asyncio
questions = [
"What was that ai app that i saw on the news the other day?",
"Can you find the trainline booking email?",
"what did I do on Monday?",
"Tell me about todays meeting and how it relates to the email on Monday",
]
resp = asyncio.run(main(questions))
for r in resp:
print("q:", r["question"])
#> q: what did I do on Monday?
print("c:", r["classification"])
#> c: ['SUMMARY']
如果你按照我们所做的,你会发现我们包装了客户端并快速使用了 asyncio 对一系列问题进行分类。这是如何使用 LangSmith 增强 OpenAI 客户端的一个简单示例。你可以使用 LangSmith 监控和观察客户端,并使用 instructor
为客户端添加额外功能。
要查看此运行的跟踪,请查看此可共享 链接。