mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
clean up docs
This commit is contained in:
@@ -4,7 +4,7 @@ For the full code example check out [examples/citation_fuzzy_match.py](https://g
|
||||
|
||||
## Overview
|
||||
|
||||
This document demonstrates how to use Instructor with validators to add citations to answers generated by AI models. The aim is to prevent hallucinations by ensuring that every statement made by the AI is backed up by a direct quote from the context provided. Two Python classes, `Fact` and `QuestionAnswer`, are defined to encapsulate the information of individual facts and the entire answer, respectively.
|
||||
This example shows how to use Instructor with validators to not only add citations to answers generated but also prevent hallucinations by ensuring that every statement made by the LLM is backed up by a direct quote from the context provided, and that those quotes exist!.Two Python classes, `Fact` and `QuestionAnswer`, are defined to encapsulate the information of individual facts and the entire answer, respectively.
|
||||
|
||||
## Data Structures
|
||||
|
||||
@@ -68,8 +68,11 @@ class QuestionAnswer(instructor.OpenAISchema):
|
||||
## Function to Ask AI a Question
|
||||
|
||||
### The `ask_ai` Function
|
||||
|
||||
This function takes a string `question` and a string `context` and returns a `QuestionAnswer` object. It uses the OpenAI API to fetch the answer and then validates the sources using the defined classes.
|
||||
|
||||
To understand the validation context work from pydantic check out [pydantic's docs](https://docs.pydantic.dev/usage/validators/#model-validators)
|
||||
|
||||
```python hl_lines="5 6 14"
|
||||
def ask_ai(question: str, context: str) -> QuestionAnswer:
|
||||
completion = openai.ChatCompletion.create(
|
||||
@@ -89,6 +92,7 @@ def ask_ai(question: str, context: str) -> QuestionAnswer:
|
||||
```
|
||||
|
||||
## Example
|
||||
dd
|
||||
Here's an example of using these classes and functions to ask a question and validate the answer.
|
||||
|
||||
```python
|
||||
|
||||
Reference in New Issue
Block a user