Skill

Extract Cited Facts from Context

FastAPI app extracting GPT-4 answers with exact, verifiable citations back to source text, streamed as SSE JSON events.

Works with fastapigpt 4

91
Spark score
out of 100
Updated 19 days ago
Version 1.15.4
Models
gpt 4gpt 4o

Add to Favorites

Why it matters

Leverage advanced AI to answer questions based on provided context, extracting precise facts and their exact citations.

Outcomes

What it gets done

01

Answer questions using a given context.

02

Extract factual information with precise citations.

03

Return extracted facts as structured JSON events.

04

Utilize Server-Sent Events (SSE) for real-time data delivery.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/inst-citationwithextraction | bash

Overview

Citation With Extraction

This FastAPI app uses GPT-4 to answer questions from a given context and extract facts as exact, cited quotes, streamed via Server-Sent Events. Use it when question-answering needs verifiable, exact-quote citations to a specific source context rather than free-form paraphrased answers.

What it does

This FastAPI application uses GPT-4 to answer questions from a given context and extract relevant facts as exact, correctly-cited quotes, streamed back as JSON events over Server-Sent Events (SSE). The /extract endpoint accepts a POST with context (the source text) and query (the question), and returns extracted facts each paired with their character-offset spans and the literal citation text drawn from the context.

When to use - and when NOT to

Use it when you need question-answering with verifiable, exact-quote citations rather than paraphrased or hallucinated support - the app is built specifically to ensure extracted facts are direct quotes from the source context. Do not use it if you need answers without grounding in a specific provided context, or if streaming SSE responses don't fit your client architecture.

Inputs and outputs

POST /extract with JSON {"context": "...", "query": "..."}; the response streams SSE events like data: {'body': 'In school, the author went to an arts high school.', 'spans': [(91, 106)], 'citation': ['arts highschool']} - one event per extracted fact, each with its supporting quote and exact character offsets in the source context:

data: {'body': 'In university, the author studied Computational Mathematics and physics.', 'spans': [(135, 172)], 'citation': ['Computational Mathematics and physics']}

Requires an OpenAI API key for GPT-4 access, installed via pip install -r requirements.txt.

Integrations

Run locally with uvicorn main:app --reload, serving /extract at http://localhost:8000/extract. A hosted instance is also available for testing with your own OpenAI key, without deploying the app yourself - the author states the code is public and no keys are stored:

curl -X 'POST' \
  'https://jxnl--rag-citation-fastapi-app.modal.run/extract' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <OPENAI_API_KEY>' \
  -d '{
  "context": "My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.I went to an arts highschool but in university I studied Computational Mathematics and physics.  As part of coop I worked at many companies including Stitchfix, Facebook.  I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.",
  "query": "What did the author do in school?"
}'

Licensed under the MIT License.

Who it's for

Developers building RAG or question-answering systems who need extracted facts to carry verifiable, exact citations back to the source text rather than free-form paraphrased answers.

Source README

Citation with Extraction

This repository contains a FastAPI application that uses GPT-4 to answer questions based on a given context and extract relevant facts with correct and exact citations. The extracted facts are returned as JSON events using Server-Sent Events (SSE).

How it Works

The FastAPI app defines an endpoint /extract that accepts a POST request with JSON data containing a context and a query. The context represents the text from which the question is being asked, and the query is the question itself.

The app leverages GPT-4, an advanced language model, to generate answers to the questions and extract relevant facts. It ensures that the extracted facts include direct quotes from the given context.

Example Usage

To use the /extract endpoint, send a POST request with curl or any HTTP client with the following format:

curl -X POST -H "Content-Type: application/json" -d '{
  "context": "My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.I went to an arts highschool but in university I studied Computational Mathematics and physics.  As part of coop I worked at many companies including Stitchfix, Facebook.  I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.",
  "query": "What did the author do in school?"
}' -N http://localhost:8000/extract
data: {'body': 'In school, the author went to an arts high school.', 'spans': [(91, 106)], 'citation': ['arts highschool']}
data: {'body': 'In university, the author studied Computational Mathematics and physics.', 'spans': [(135, 172)], 'citation': ['Computational Mathematics and physics']}

Replace http://localhost:8000 with the actual URL of your FastAPI app if it's running on a different host and port. The API will respond with Server-Sent Events (SSE) containing the extracted facts in real-time.

Bring your own API key

If you have your own api key but dont want to try deploying it yourself you're welcome to use my
modal isntance here, this code is public and I do not store your key.

curl -X 'POST' \
  'https://jxnl--rag-citation-fastapi-app.modal.run/extract' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <OPENAI_API_KEY>' \
  -d '{
  "context": "My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.I went to an arts highschool but in university I studied Computational Mathematics and physics.  As part of coop I worked at many companies including Stitchfix, Facebook.  I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.",
  "query": "What did the author do in school?"
}'

Requirements

To run this application, ensure you have the following Python packages installed:

pip install -r requirements.txt

Running the App

To run the FastAPI app, execute the following command:

uvicorn main:app --reload

This will start the server, and the /extract endpoint will be available at http://localhost:8000/extract.

Note

Ensure that you have a valid API key for GPT-4 from OpenAI. If you don't have one, you can obtain it from the OpenAI website.

Please use this application responsibly and be mindful of any usage limits or restrictions from OpenAI's API usage policy.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.