Coder Social home page Coder Social logo

auto-evaluator's Introduction

Auto-evaluator ๐Ÿง  ๐Ÿ“

Context

Document Question-Answering is a popular LLM use-case. LangChain makes it easy to assemble LLM components (e.g., models and retrievers) into chains that support question-answering: input documents are split into chunks and stored in a retriever, relevant chunks are retrieved given a user question and passed to an LLM for synthesis into an answer.

Challenge

The quality of QA systems can vary considerably; for example, we have seen cases of hallucination and poor answer quality due specific parameter settings. But, it is not always obvious to (1) evaluate the answer quality in a systematic way and (2) use this evaluation to guide improved QA chain settings (e.g., chunk size) or components (e.g., model or retriever choice).

App overview

This app aims to address the above limitations. Recent work from Anthropic has used model-written evaluation sets. OpenAI and others have shown that model-graded evaluation is an effective way to evaluate models. This app combines both of these ideas into a single workspace, auto-generating a QA test set and auto-grading the result of the specified QA chain.

image

Usage

The app can be used in two ways:

  • Demo: We pre-loaded a document (a transcript of the Lex Fridman podcast with Andrej Karpathy) and a set of 5 question-answer pairs from the podcast. You can configure QA chain(s) and run an experiment.

image

  • Playground: Input a set of documents that you want to ask questions about. Optionally, also include your own test set of question-answer pairs related to the documents; see an example here. If you do not supply a test set, the app will auto-generate one. If the test set is smaller than the desired number of eval questions specified in the top left, the app will auto-generate the remainder.

image

Building the document retrieval:

  • The app will build a retriever for the input documents.
  • Retriever is a Langchain abstraction that accepts a question and returns a set of relevant documents.
  • The retriever can be selected by the user in the drop-down list in the configurations (red panel above).

Test set generation:

  • The app will auto-generate a test set of question-answer pair on the doc(s).
  • To do this, it uses the Langchain QAGenerationChain with the default prompt here.

LLM question-answering:

  • For each question, we use a RetrievalQA chain to answer it.
  • This will fetch chunks that are relevant to the question from the retriever and pass them to the LLM.
  • We expose the QA_CHAIN_PROMPT used for to pass this context to the LLM here.

Model-graded evaluation:

  • We let the user select from a number of model-graded evaluation prompts:

(1) The app will evaluate the relevance of the retrieved documents relative to the question.

(2) The app will evaluate the similarity of the LLM generated answer relative to ground truth answer.

  • The prompts for both can be seen here
  • Users can select which grading prompt to use. Here are some notes in prompt selection from our experience.

Experimental results:

  • The app will produce a table summarizing the results.
  • It shows the question and the ground truth (expected) answer.
  • It shows the chain-generated answer.
  • It shows the binary score (PASS / FAIL) for retrieval and the answer.
  • It shows the latency for retrieval and LLM answer summarization per question.
  • It shows the model grader output (the raw output of the grading prompt).

image

User inputs

The left panel of the app (shown in red in the above image) has several user-configurable parameters.

Number of eval questions - This is the number of question-answer pairs to auto-generate for the given inputs documents. As mentioned above, question-answer pair auto-generation will use Langchain's QAGenerationChain with prompt specified here.

Chunk size - Number of characters per chunk when the input documents are split. This can impact answer quality. Retrievers often use text embedding similarity to select chunks related to the question. If the chunks are too large, each chunk may contain more information unrelated to the question, which may degrade the summarized answer quality. If chunks are too small, important context may be left out of the retrieved chunks.

Overlap - The overlap in characters between chunks.

Embedding - The method used to embed chunks.

Retriever - The method used to retrieve chunks that are relevant to the user question. The default vector database used for similarity search is FAISS, but support for others is a welcome addition. You can also try other methods, such as SVM or TF-IDF.

Number of chunks to retrieve - Number of chunks retrieved. More chunks can improve performance by giving the LLM more context for answer summarization.

Model - LLM for summarization of retrieved chunks into the answer.

Grading prompt style - The prompt choice for model-graded evaluation. As mentioned above, the prompts can be seen here. More prompts would be a welcome addition. For example, with the Descriptive prompt, you will see a more detailed output with model grade justification.

Logging experiments

A user can select the desired configuration and then choose Re-Run Experiment.

This will run the new chain on the existing test set.

The results from all experiments will be summarized in the table and chart.

image

Contributing

Run the backend from api folder:

 pip install -r requirements.txt
 uvicorn evaluator_app:app

Test the api locally:

curl -X POST -F "files=@docs/karpathy-lex-pod/karpathy-pod.txt" -F "num_eval_questions=1" -F "chunk_chars=1000" -F "overlap=100" -F "split_method=RecursiveTextSplitter" -F "retriever_type=similarity-search" -F "embeddings=OpenAI" -F "model_version=gpt-3.5-turbo" -F "grade_prompt=Fast" -F "num_neighbors=3" http://localhost:8000/evaluator-stream

Run the frontend from nextjs folder and view web app at specified URL (e.g., http://localhost:3000/):

yarn install
yarn dev

Environment Variables

Front-end:

.env.local contains the env variables needed to run the project.

Back-end:

Specify the API keys for any models that you want to use.

OPENAI_API_KEY=
ANTHROPIC_API_KEY=

Deployment

The front-end is deployed to Vercel.

The back-end is deployed to Railway.

auto-evaluator's People

Contributors

avibanerjee avatar barniker avatar benisgold avatar dankolesnikov avatar hwchase17 avatar rlancemartin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

auto-evaluator's Issues

Back-end crashing

Reports crashed -

image

Server crashed -

image

Logging is not obvious -

INFO:     Uvicorn running on http://0.0.0.0:7106/ (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://0.0.0.0:7106/ (Press CTRL+C to quit)
INFO:     192.168.0.2:33390 - "POST /evaluator-stream HTTP/1.1" 200 OK
2023-05-05 09:04:42,541 loglevel=INFO   logger=evaluator_app run_evaluator() L334  Reading file: PosteVivereProtetti_CGA.pdf
2023-05-05 09:04:42,544 loglevel=INFO   logger=evaluator_app run_evaluator() L338  File PosteVivereProtetti_CGA.pdf is a PDF
2023-05-05 09:05:01,346 loglevel=INFO   logger=evaluator_app run_evaluator() L355  Splitting texts
2023-05-05 09:05:01,347 loglevel=INFO   logger=evaluator_app split_texts() L80   `Splitting doc ...`
2023-05-05 09:05:01,375 loglevel=INFO   logger=evaluator_app run_evaluator() L358  Make LLM
model!
gpt-4
2023-05-05 09:05:01,376 loglevel=INFO   logger=evaluator_app run_evaluator() L361  Make retriever
2023-05-05 09:05:01,376 loglevel=INFO   logger=evaluator_app make_retriever() L120  `Making retriever ...`
2023-05-05 09:05:07,734 loglevel=INFO   logger=evaluator_app run_evaluator() L365  Make chain
2023-05-05 09:05:07,735 loglevel=INFO   logger=evaluator_app generate_eval() L46   `Generating eval QA pair ...`
2023-05-05 09:05:32,805 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:05:50,387 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:05:55,938 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
INFO:     192.168.0.4:36928 - "POST /evaluator-stream HTTP/1.1" 200 OK
2023-05-05 09:06:33,392 loglevel=INFO   logger=evaluator_app run_evaluator() L334  Reading file: karpathy-pod.txt
2023-05-05 09:06:33,392 loglevel=INFO   logger=evaluator_app run_evaluator() L347  File karpathy-pod.txt is a TXT
2023-05-05 09:06:33,392 loglevel=INFO   logger=evaluator_app run_evaluator() L355  Splitting texts
2023-05-05 09:06:33,392 loglevel=INFO   logger=evaluator_app split_texts() L80   `Splitting doc ...`
2023-05-05 09:06:33,506 loglevel=INFO   logger=evaluator_app run_evaluator() L358  Make LLM
model!
gpt-3.5-turbo
2023-05-05 09:06:33,506 loglevel=INFO   logger=evaluator_app run_evaluator() L361  Make retriever
2023-05-05 09:06:33,506 loglevel=INFO   logger=evaluator_app make_retriever() L120  `Making retriever ...`
2023-05-05 09:06:35,081 loglevel=INFO   logger=evaluator_app run_evaluator() L365  Make chain
2023-05-05 09:06:35,082 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:06:38,116 loglevel=INFO   logger=evaluator_app generate_eval() L46   `Generating eval QA pair ...`
2023-05-05 09:06:48,428 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:06:57,001 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:07:00,491 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:07:02,402 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:07:04,823 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:07:15,961 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:07:22,977 loglevel=INFO   logger=evaluator_app generate_eval() L46   `Generating eval QA pair ...`
2023-05-05 09:07:26,002 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:07:38,376 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:07:39,099 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:07:47,511 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
INFO:     192.168.0.4:56978 - "POST /evaluator-stream HTTP/1.1" 200 OK
2023-05-05 09:07:50,268 loglevel=INFO   logger=evaluator_app run_evaluator() L334  Reading file: karpathy-pod.txt
2023-05-05 09:07:50,269 loglevel=INFO   logger=evaluator_app run_evaluator() L347  File karpathy-pod.txt is a TXT
2023-05-05 09:07:50,269 loglevel=INFO   logger=evaluator_app run_evaluator() L355  Splitting texts
2023-05-05 09:07:50,269 loglevel=INFO   logger=evaluator_app split_texts() L80   `Splitting doc ...`
2023-05-05 09:07:50,386 loglevel=INFO   logger=evaluator_app run_evaluator() L358  Make LLM
model!
gpt-4
2023-05-05 09:07:50,386 loglevel=INFO   logger=evaluator_app run_evaluator() L361  Make retriever
2023-05-05 09:07:50,386 loglevel=INFO   logger=evaluator_app make_retriever() L120  `Making retriever ...`
2023-05-05 09:07:50,743 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:07:51,940 loglevel=INFO   logger=evaluator_app run_evaluator() L365  Make chain
2023-05-05 09:07:51,940 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:08:01,489 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:08:09,404 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:08:14,427 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:08:24,486 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:08:28,668 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:08:35,293 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
INFO:     192.168.0.4:60550 - "POST /evaluator-stream HTTP/1.1" 200 OK
2023-05-05 09:08:45,730 loglevel=INFO   logger=evaluator_app run_evaluator() L334  Reading file: PosteVivereProtetti_CGA.pdf
2023-05-05 09:08:45,731 loglevel=INFO   logger=evaluator_app run_evaluator() L338  File PosteVivereProtetti_CGA.pdf is a PDF
2023-05-05 09:08:49,638 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:08:55,964 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:08:59,938 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:09:04,879 loglevel=INFO   logger=evaluator_app run_evaluator() L355  Splitting texts
2023-05-05 09:09:04,879 loglevel=INFO   logger=evaluator_app split_texts() L80   `Splitting doc ...`
2023-05-05 09:09:04,906 loglevel=INFO   logger=evaluator_app run_evaluator() L358  Make LLM
model!
gpt-4
2023-05-05 09:09:04,906 loglevel=INFO   logger=evaluator_app run_evaluator() L361  Make retriever
2023-05-05 09:09:04,906 loglevel=INFO   logger=evaluator_app make_retriever() L120  `Making retriever ...`
2023-05-05 09:09:16,493 loglevel=INFO   logger=evaluator_app grade_model_answer() L177  `Grading model answer ...`
2023-05-05 09:09:20,289 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L205  `Grading relevance of retrieved docs ...`
2023-05-05 09:09:24,466 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`
2023-05-05 09:09:30,584 loglevel=INFO   logger=evaluator_app run_evaluator() L365  Make chain
2023-05-05 09:09:30,585 loglevel=INFO   logger=evaluator_app run_eval() L236  `Running eval ...`

Prod (likely) hitting OOM

TL;DR we see OOM (or server returning

These are 15 large pdfs fail (503 server error) on prod, but work on local:

curl -X POST \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "num_eval_questions=1" \
-F "chunk_chars=1000" \
-F "overlap=100" \
-F "split_method=RecursiveTextSplitter" \
-F "retriever_type=similarity-search" \
-F "embeddings=OpenAI" \
-F "model_version=gpt-3.5-turbo" \
-F "grade_prompt=Fast" \
-F "num_neighbors=3" \
https://auto-evaluator-production.up.railway.app/evaluator-stream

The first 7 work on prod w/ similarity-search:

curl -X POST \                                                                                        
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "num_eval_questions=1" \
-F "chunk_chars=1000" \
-F "overlap=100" \
-F "split_method=RecursiveTextSplitter" \
-F "retriever_type=similarity-search" \
-F "embeddings=OpenAI" \
-F "model_version=gpt-3.5-turbo" \
-F "grade_prompt=Fast" \
-F "num_neighbors=3" \
https://auto-evaluator-production.up.railway.app/evaluator-stream

They are ~17 MB.

Adding any additional files fail (e.g., below fails):

curl -X POST \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]" \
-F "num_eval_questions=1" \
-F "chunk_chars=1000" \
-F "overlap=100" \
-F "split_method=RecursiveTextSplitter" \
-F "retriever_type=similarity-search" \
-F "embeddings=OpenAI" \
-F "model_version=gpt-3.5-turbo" \
-F "grade_prompt=Fast" \
-F "num_neighbors=3" \
https://auto-evaluator-production.up.railway.app/evaluator-stream

Add instructions to the demo landing page

Top (Text box, grey background)

Welcome to the auto-evaluator! This is an app to evaluate the performance of question-answering LLM chains. This demo has pre-loaded two things: (1) a document (the Lex Fridman podcast with Andrej Karpathy) and (2) a "test set" of question-answer pairs for this episode. The aim to evaluate the performance of various question-answering LLM chain configuration against test set. You can build any QA chain using on the components and score its performance.

Button (Text box, green background)

Choose the question-answering chain configuration (left) and launch an experiment using the button below. For more detail on each setting, see full the documentation here.

  • Color: green
  • Title: run experiment

Summary

  • Re-name initial row as baseline

Experiment Results (Text box, grey background)

This table shows the each question-answer pair from the test set along with the model's answer to the question. The app will score two things: (1) the relevance of the retrieved documents relative to the question and (2) the similarity of the LLM generated answer relative to ground truth answer. The prompts for both can be seen here and can be chosen by the user in the drop-down list Grading prompt style. The FAST prompt will only have the LLM grader output the score. The other prompts will also produce an explanation.

Fails to run w/ Llama-Ix

image

And gives the incorrect Insight
The experiment that performed the best was Experiment #2 due to combination of accuracy and latency.

Catch server errors and return alert to the user

Test w/ GPT-4 paper (example of invalid token):

ValueError: Encountered text corresponding to disallowed special token '<|endofprompt|>'.
If you want this text to be encoded as a special token, pass it to `allowed_special`, e.g. `allowed_special={'<|endofprompt|>', ...}`.
If you want this text to be encoded as normal text, disable the check for this token by passing `disallowed_special=(enc.special_tokens_set - {'<|endofprompt|>'})`.
To disable this check for all special tokens, pass `disallowed_special=()`.

Bug in the FAST scoring

Bad answer scored as PASS:
image

But using DETAILED prompt, it is corrected marked as INCORRECT:
image

Missing logging / alert when back-end crashes

Back-end crashed according to Railway.

Did not see clear logging / alert in Sentry.

Last logging:

2023-05-02 17:31:03,008 loglevel=INFO   logger=evaluator_app grade_model_retrieval() L206  `Grading relevance of retrieved docs ...`
INFO:     Shutting down
2023-05-02 17:38:48,691 loglevel=INFO   logger=uvicorn.error shutdown() L253  Shutting down
INFO:     Waiting for application shutdown.
2023-05-02 17:38:48,793 loglevel=INFO   logger=uvicorn.error shutdown() L66   Waiting for application shutdown.
INFO:     Application shutdown complete.
2023-05-02 17:38:48,793 loglevel=INFO   logger=uvicorn.error shutdown() L77   Application shutdown complete.
INFO:     Finished server process [1]
2023-05-02 17:38:48,793 loglevel=INFO   logger=uvicorn.error serve() L85   Finished server process [1]

Not quite obvious why it crashed based on this logging.

Remove Doppler dep

Still need Doppler for local testing.

Should we remove?

And for local tested we should create a .env template for folks for follow (e.g., with all required API keys).

Show intermediate states in the loading bar

How much effort to make the "processing files" stage more granular (e.g., show making retriever, making eval set, etc)? would be slightly better UX b/c this stage does hang for ~20-30 sec (esp if making eval set, etc).

image

Anthropic model appears to be deprecated

/opt/venv/lib/python3.8/site-packages/langchain/llms/anthropic.py:130: UserWarning: This Anthropic LLM is deprecated. Please use `from langchain.chat_models import ChatAnthropic` instead

Unbreak

In 7b3ef16 we removed doppler -

"dev": "doppler run -- next dev",

Removal of doppler means that some env variables were missing.

Both local (main) and prod are broken.

Adding these env variables to .env.local fixes local -

NEXT_PUBLIC_EVALUATOR_API_URL="http://127.0.0.1:8000"
NEXT_PUBLIC_API_URL="http://127.0.0.1:8000"

What is required for prod to work?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.