跳到主要內容
Open In ColabOpen on GitHub

Fiddler

Fiddler 是企業生成式和預測系統運營的先驅,提供統一平台,使資料科學、MLOps、風險、合規、分析和其他業務線團隊能夠以企業規模監控、解釋、分析和改進 ML 部署。

1. 安裝與設定

#!pip install langchain langchain-community langchain-openai fiddler-client

2. Fiddler 連線詳細資訊

在您可以使用 Fiddler 新增有關模型的資訊之前

  1. 您用來連線到 Fiddler 的 URL
  2. 您的組織 ID
  3. 您的授權權杖

這些資訊可以在您的 Fiddler 環境的「設定」頁面中找到。

URL = ""  # Your Fiddler instance URL, Make sure to include the full URL (including https://). For example: https://demo.fiddler.ai
ORG_NAME = ""
AUTH_TOKEN = "" # Your Fiddler instance auth token

# Fiddler project and model names, used for model registration
PROJECT_NAME = ""
MODEL_NAME = "" # Model name in Fiddler

3. 建立 Fiddler 回呼處理常式執行個體

from langchain_community.callbacks.fiddler_callback import FiddlerCallbackHandler

fiddler_handler = FiddlerCallbackHandler(
url=URL,
org=ORG_NAME,
project=PROJECT_NAME,
model=MODEL_NAME,
api_key=AUTH_TOKEN,
)

範例 1:基本鏈

from langchain_core.output_parsers import StrOutputParser
from langchain_openai import OpenAI

# Note : Make sure openai API key is set in the environment variable OPENAI_API_KEY
llm = OpenAI(temperature=0, streaming=True, callbacks=[fiddler_handler])
output_parser = StrOutputParser()

chain = llm | output_parser

# Invoke the chain. Invocation will be logged to Fiddler, and metrics automatically generated
chain.invoke("How far is moon from earth?")
API 參考:StrOutputParser | OpenAI
# Few more invocations
chain.invoke("What is the temperature on Mars?")
chain.invoke("How much is 2 + 200000?")
chain.invoke("Which movie won the oscars this year?")
chain.invoke("Can you write me a poem about insomnia?")
chain.invoke("How are you doing today?")
chain.invoke("What is the meaning of life?")

範例 2:具有提示範本的鏈

from langchain_core.prompts import (
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
)

examples = [
{"input": "2+2", "output": "4"},
{"input": "2+3", "output": "5"},
]

example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{input}"),
("ai", "{output}"),
]
)

few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)

final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a wondrous wizard of math."),
few_shot_prompt,
("human", "{input}"),
]
)

# Note : Make sure openai API key is set in the environment variable OPENAI_API_KEY
llm = OpenAI(temperature=0, streaming=True, callbacks=[fiddler_handler])

chain = final_prompt | llm

# Invoke the chain. Invocation will be logged to Fiddler, and metrics automatically generated
chain.invoke({"input": "What's the square of a triangle?"})

此頁面是否對您有幫助?