mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
add the fold
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
---
|
||||
draft: False
|
||||
draft: False
|
||||
date: 2023-11-02
|
||||
tags:
|
||||
- python
|
||||
@@ -12,10 +12,12 @@ authors:
|
||||
|
||||
# AI Engineer Keynote: Pydantic is all you need
|
||||
|
||||
[](https://www.youtube.com/watch?v=yj-wSRJwrrc)
|
||||
[](https://www.youtube.com/watch?v=yj-wSRJwrrc)
|
||||
|
||||
[Click here to watch the full talk](https://www.youtube.com/watch?v=yj-wSRJwrrc)
|
||||
|
||||
Last month, I ventured back onto the speaking circuit at the inaugural [AI Engineer Summit](https://www.ai.engineer/summit), sharing insights on leveraging [Pydantic](https://docs.pydantic.dev/latest/) for effective prompt engineering. I dove deep into what is covered in our documentation and standard blog posts,
|
||||
<!-- more -->
|
||||
|
||||
Last month, I ventured back onto the speaking circuit at the inaugural [AI Engineer Summit](https://www.ai.engineer/summit), sharing insights on leveraging [Pydantic](https://docs.pydantic.dev/latest/) for effective prompt engineering. I dove deep into what is covered in our documentation and standard blog posts,
|
||||
|
||||
I'd genuinely appreciate any feedback on the talk – every bit helps in refining the art. So, take a moment to check out the [full talk here](https://youtu.be/yj-wSRJwrrc?si=vGMIqtTapbIN8SLz), and let's continue pushing the boundaries of what's possible.
|
||||
|
||||
@@ -16,6 +16,8 @@ Open-source LLMS are gaining popularity, and the release of Anyscale's Mistral m
|
||||
|
||||
By the end of this blog post, you will learn how to effectively utilize the instructor at any scale. But before we proceed, let's first explore the concept of patching.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
## Patching
|
||||
|
||||
Instructor's patch enhances a openai api it with the following features:
|
||||
|
||||
@@ -18,6 +18,8 @@ authors:
|
||||
|
||||
Today, we're diving into optimizing instructor code while maintaining the excellent DX offered by [Pydantic](https://docs.pydantic.dev/latest/) models. We'll tackle the challenges of caching Pydantic models, typically incompatible with `pickle`, and explore solutions that use `decorators` like `functools.cache`. Then, we'll craft custom decorators with `diskcache` and `redis` to support persistent caching and distributed systems.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
Lets first consider our canonical example, using the `OpenAI` Python client to extract user details.
|
||||
|
||||
```python
|
||||
|
||||
@@ -22,6 +22,8 @@ In this article, we'll guide you through implementing the original Chain of Dens
|
||||
|
||||
By the end you'll end up with a GPT 3.5 model, (fine-tuned using Instructor's great tooling), capable of producing summaries that rival the effectiveness of Chain of Density [[Adams et al. (2023)]](https://arxiv.org/abs/2309.04269). As always, all code is readily available in our `examples/chain-of-density` folder in our repo for your reference.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
??? abstract "Datasets and Colab Notebook"
|
||||
|
||||
We've also uploaded all our generated data to Hugging Face [here](https://huggingface.co/datasets/ivanleomk/gpt4-chain-of-density) for you to use if you'd like to try reproducing these experiments. We've also added a [Colab Instance](https://colab.research.google.com/drive/1iBkrEh2G5U8yh8RmI8EkWxjLq6zIIuVm?usp=sharing) for you to check our generated values.
|
||||
@@ -150,7 +152,7 @@ Firstly, we'll need a data model for the initial summary that we will be generat
|
||||
"required": [
|
||||
"summary"
|
||||
]
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -18,6 +18,8 @@ Ensuring the accuracy of information is crucial. This blog post explores how Pyd
|
||||
|
||||
We'll start with using a simple substring check to verify citations. Then we'll use `instructor` itself to power an LLM to verify citations and align answers with the given citations. Finally, we'll explore how we can use these techniques to generate a dataset of accurate responses.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
## Example 1: Simple Substring Check
|
||||
|
||||
In this example, we use the `Statements` class to verify if a given substring quote exists within a text chunk. If the substring is not found, an error is raised.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
draft: False
|
||||
draft: False
|
||||
date: 2023-10-17
|
||||
tags:
|
||||
- python
|
||||
@@ -14,11 +14,13 @@ authors:
|
||||
|
||||
## Introduction
|
||||
|
||||
Get ready to dive deep into the world of fine-tuning task specific language models with Python functions. We'll explore how the `instructor.instructions` streamlines this process, making the task you want to distil more efficient and powerful while preserving its original functionality and backwards compatibility.
|
||||
Get ready to dive deep into the world of fine-tuning task specific language models with Python functions. We'll explore how the `instructor.instructions` streamlines this process, making the task you want to distil more efficient and powerful while preserving its original functionality and backwards compatibility.
|
||||
|
||||
If you want to see the full example checkout [examples/distillation](https://github.com/jxnl/instructor/tree/main/examples/distilations)
|
||||
|
||||
## Why use Instructor?
|
||||
<!-- more -->
|
||||
|
||||
## Why use Instructor?
|
||||
|
||||
Imagine you're developing a backend service that uses a mix old and new school ML practises, it may involve pipelines with multiple function calls, validations, and data processing. Sounds cumbersome, right? That's where `Instructor` comes in. It simplifies complex procedures, making them more efficient and easier to manage by adding a decorator to your function that will automatically generate a dataset for fine-tuning and help you swap out the function implementation.
|
||||
|
||||
@@ -40,7 +42,7 @@ instructions = Instructions(
|
||||
finetune_format="messages",
|
||||
# log handler is used to save the data to a file
|
||||
# you can imagine saving it to a database or other storage
|
||||
# based on your needs!
|
||||
# based on your needs!
|
||||
log_handlers=[logging.FileHandler("math_finetunes.jsonl")]
|
||||
)
|
||||
|
||||
@@ -80,6 +82,7 @@ The library offers two main benefits:
|
||||
The `from instructor import Instructions` feature is a time saver. It auto-generates a fine-tuning dataset, making it a breeze to imitate a function's behavior.
|
||||
|
||||
## Logging Output and Running a Finetune
|
||||
|
||||
Here's how the logging output would look:
|
||||
|
||||
```python
|
||||
@@ -87,10 +90,10 @@ Here's how the logging output would look:
|
||||
"messages": [
|
||||
{"role": "system", "content": 'Predict the results of this function: ...'},
|
||||
{"role": "user", "content": 'Return fn(133, b=539)'},
|
||||
{"role": "assistant",
|
||||
"function_call":
|
||||
{"role": "assistant",
|
||||
"function_call":
|
||||
{
|
||||
"name": "Multiply",
|
||||
"name": "Multiply",
|
||||
"arguments": '{"a":133,"b":539,"result":89509}'
|
||||
}
|
||||
}
|
||||
@@ -112,8 +115,8 @@ instructor jobs create-from-file math_finetunes.jsonl
|
||||
```
|
||||
|
||||
## Next Steps and Future Plans
|
||||
Here's a sneak peek of what I'm planning:
|
||||
|
||||
Here's a sneak peek of what I'm planning:
|
||||
|
||||
```python
|
||||
from instructor import Instructions, patch
|
||||
@@ -135,7 +138,6 @@ def fn(a: int, b: int) -> Multiply:
|
||||
return Multiply(a=a, b=b, result=resp)
|
||||
```
|
||||
|
||||
|
||||
1. Don't forget to run the `patch()` command that we provide with the `Instructor` package. This helps
|
||||
automatically serialize the content back into the `Pydantic`` model that we're looking for.
|
||||
|
||||
@@ -148,5 +150,4 @@ With this, you can swap the function implementation, making it backward compatib
|
||||
|
||||
We've seen how `Instructor` can make your life easier, from fine-tuning to distillation. Now if you're thinking wow, I'd love a backend service to do this for continously, you're in luck! Please check out the survey at [useinstructor.com](https://useinstructor.com) and let us know who you are.
|
||||
|
||||
|
||||
If you enjoy the content or want to try out `instructor` please check out the [github](https://github.com/jxnl/instructor) and give us a star!
|
||||
If you enjoy the content or want to try out `instructor` please check out the [github](https://github.com/jxnl/instructor) and give us a star!
|
||||
|
||||
@@ -17,6 +17,8 @@ Latency is crucial, especially in eCommerce and newer chat applications like Cha
|
||||
|
||||
And what makes streaming possible? Generators!
|
||||
|
||||
<!-- more -->
|
||||
|
||||
In this post, we're going to dive into the cool world of Python generators — these tools are more than just a coding syntax trick. We'll explore Python generators from the ground up and then delve into LLM streaming using the Instructor library.
|
||||
|
||||
## Python Generators: An Efficient Approach to Iterables
|
||||
@@ -260,18 +262,17 @@ Time for first result (list): 8.63 seconds
|
||||
|
||||
Our web application now displays results faster. Even a 100ms improvement can lead to a 1% increase in revenue.
|
||||
|
||||
|
||||
### FastAPI
|
||||
We can also take this and set up a streaming LLM API endpoint using FastAPI. Check out our docs on using FastAPI [here](../../concepts/fastapi.md)!
|
||||
|
||||
We can also take this and set up a streaming LLM API endpoint using FastAPI. Check out our docs on using FastAPI [here](../../concepts/fastapi.md)!
|
||||
|
||||
## Key Takeaways
|
||||
|
||||
To summarize, we looked at:
|
||||
|
||||
• Generators in Python: A powerful feature that allows for efficient data handling with reduced latency
|
||||
• Generators in Python: A powerful feature that allows for efficient data handling with reduced latency
|
||||
|
||||
• LLM Streaming: LLMs provide us generators to stream tokens and Instructor can let us validate and extract data from this stream. Real-time data validation ftw!
|
||||
• LLM Streaming: LLMs provide us generators to stream tokens and Instructor can let us validate and extract data from this stream. Real-time data validation ftw!
|
||||
|
||||
Don't forget to check our [GitHub](https://github.com/jxnl/instructor) for more resources and give us a star if you find the library helpful!
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ authors:
|
||||
|
||||
Language models have seen significant growth. Using them effectively often requires complex frameworks. This post discusses how Instructor simplifies this process using Pydantic.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
## The Problem with Existing LLM Frameworks
|
||||
|
||||
Current frameworks for Language Learning Models (LLMs) have complex setups. Developers find it hard to control interactions with language models. Some frameworks require complex JSON Schema setups.
|
||||
|
||||
@@ -16,6 +16,8 @@ authors:
|
||||
|
||||
Today, I will introduce you to various approaches for using asyncio in Python. We will apply this to batch process data using `instructor` and learn how to use `asyncio.gather` and `asyncio.as_completed` for concurrent data processing. Additionally, we will explore how to limit the number of concurrent requests to a server using `asyncio.Semaphore`.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
!!! notes "Github Example"
|
||||
|
||||
If you want to run the code examples in this article, you can find them on [jxnl/instructor](https://github.com/jxnl/instructor/blob/main/examples/learn-async/run.py)
|
||||
|
||||
@@ -16,7 +16,8 @@ authors:
|
||||
With the advent of large language models (LLM), retrival augmented generation (RAG) has become a hot topic. However throught the past year of [helping startups](https://jxnl.co) integrate LLMs into their stack I've noticed that the pattern of taking user queries, embedding them, and directly searching a vector store is effectively demoware.
|
||||
|
||||
!!! note "What is RAG?"
|
||||
Retrival augmented generation (RAG) is a technique that uses an LLM to generate responses, but uses a search backend to augment the generation. In the past year using text embeddings with a vector databases has been the most popular approach I've seen being socialized.
|
||||
|
||||
Retrival augmented generation (RAG) is a technique that uses an LLM to generate responses, but uses a search backend to augment the generation. In the past year using text embeddings with a vector databases has been the most popular approach I've seen being socialized.
|
||||
|
||||
<figure markdown>
|
||||

|
||||
@@ -25,6 +26,8 @@ Retrival augmented generation (RAG) is a technique that uses an LLM to generate
|
||||
|
||||
So let's kick things off by examining what I like to call the 'Dumb' RAG Model—a basic setup that's more common than you'd think.
|
||||
|
||||
<!-- more -->
|
||||
|
||||
## The 'Dumb' RAG Model
|
||||
|
||||
When you ask a question like, "what is the capital of France?" The RAG 'dumb' model embeds the query and searches in some unopinonated search endpoint. Limited to a single method API like `search(query: str) -> List[str]`. This is fine for simple queries, since you'd expect words like 'paris is the capital of france' to be in the top results of say, your wikipedia embeddings.
|
||||
|
||||
@@ -26,6 +26,8 @@ def validation_function(value):
|
||||
return mutation(value)
|
||||
```
|
||||
|
||||
<!-- more -->
|
||||
|
||||
## What is Instructor?
|
||||
|
||||
`Instructor` helps to ensure you get the exact response type you're looking for when using openai's function call api. Once you've defined the `Pydantic` model for your desired response, `Instructor` handles all the complicated logic in-between - from the parsing/validation of the response to the automatic retries for invalid responses. This means that we can build in validators 'for free' and have a clear separation of concerns between the prompt and the code that calls openai.
|
||||
|
||||
Reference in New Issue
Block a user