forked from langchain-ai/chat-langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Eval Script (langchain-ai#164)
Add a branch so everything is encapsulated in the runnable. Expose single "get_chain()" function to make it easier to evaluate whatever is in main.py
- Loading branch information
Showing
2 changed files
with
92 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# TODO: Consolidate all these scripts into a single script | ||
# This is ugly | ||
import argparse | ||
import functools | ||
import json | ||
|
||
from langchain import load as langchain_load | ||
from langchain.chat_models import ChatAnthropic, ChatOpenAI | ||
from langchain.smith import RunEvalConfig | ||
from langsmith import Client, RunEvaluator | ||
from langsmith.evaluation.evaluator import EvaluationResult | ||
from langsmith.schemas import Example, Run | ||
|
||
# Ugly. Requires PYTHONATH=$(PWD) to run | ||
from main import create_chain, get_retriever | ||
|
||
_PROVIDER_MAP = { | ||
"openai": ChatOpenAI, | ||
"anthropic": ChatAnthropic, | ||
} | ||
|
||
_MODEL_MAP = { | ||
"openai": "gpt-3.5-turbo-16k", | ||
"anthropic": "claude-2", | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--dataset-name", default="Chat LangChain Complex Questions") | ||
parser.add_argument("--model-provider", default="openai") | ||
args = parser.parse_args() | ||
client = Client() | ||
# Check dataset exists | ||
ds = client.read_dataset(dataset_name=args.dataset_name) | ||
retriever = get_retriever() | ||
llm = _PROVIDER_MAP[args.model_provider]( | ||
model=_MODEL_MAP[args.model_provider], temperature=0 | ||
) | ||
|
||
# In app, we always pass in a chat history, but for evaluation we don't | ||
# necessarily do that. Add that handling here. | ||
def construct_eval_chain(): | ||
chain = create_chain( | ||
retriever=retriever, | ||
llm=llm, | ||
) | ||
return { | ||
"question": lambda x: x["question"], | ||
"chat_history": (lambda x: x.get("chat_history", [])), | ||
} | chain | ||
|
||
eval_config = RunEvalConfig( | ||
evaluators=["qa"], | ||
prediction_key="output", | ||
) | ||
results = client.run_on_dataset( | ||
dataset_name=args.dataset_name, | ||
llm_or_chain_factory=construct_eval_chain, | ||
evaluation=eval_config, | ||
tags=["simple_chain"], | ||
verbose=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters