Skip to content
Snippets Groups Projects
Commit bbbdb20d authored by Adrien Klose's avatar Adrien Klose
Browse files

work through tutorial on function calling

parent 7ef0ba0a
No related branches found
No related tags found
No related merge requests found
**/*.env
%% Cell type:code id: tags:
``` python
import os
import instructor
from groq import Groq
from pydantic import BaseModel
import dotenv
from typing_extensions import Literal
from enum import Enum
dotenv.load_dotenv()
```
%% Output
True
%% Cell type:code id: tags:
``` python
client = Groq(
api_key=os.environ.get("GROQ_API_KEY"),
)
```
%% Cell type:code id: tags:
``` python
# By default, the patch function will patch the ChatCompletion.create and ChatCompletion.create methods to support the response_model parameter
client = instructor.from_groq(client, mode=instructor.Mode.TOOLS)
```
%% Cell type:code id: tags:
``` python
# Can use the response_model parameter using the BaseModel
#class QuestionType(Enum):
# YesNo: Literal["Yes","No"]
# Factoid: str
# List: list[str]
# Summary: str
class YesNoModel(BaseModel):
answer: Literal["Yes","No"]
```
%% Cell type:code id: tags:
``` python
yesno: YesNoModel = client.chat.completions.create(
model="mixtral-8x7b-32768",
response_model=YesNoModel,
messages=[
{"role": "system", "content": "You are a helpful medical expert who answers with either yes or no to questions."},
{"role": "user", "content": "Is it safe to take isotretinoin during pregnancy?"},
],
)
assert isinstance(yesno, YesNoModel), "Should be instance of UserExtract"
```
%% Cell type:code id: tags:
``` python
print(yesno)
```
%% Output
answer='No'
%% Cell type:code id: tags:
``` python
# Not sure I understand the use of Field
from pydantic import Field
class NLSentenceModel(BaseModel):
sentence: str = Field(...,description="Must be natural language sentences")
# Could include a validator to check whether the result is a single sentence.
sent: NLSentenceModel = client.chat.completions.create(
model="mixtral-8x7b-32768",
response_model=NLSentenceModel,
messages=[
{"role": "system", "content": "You are a helpful medical expert who reformulates triples of subjec, relationship and object into one sentence."},
{"role": "user", "content": "isotretinoin has_active_gen pregnancy"},
],
)
```
%% Cell type:code id: tags:
``` python
print(sent)
```
%% Output
sentence='Isotretinoin has an active genetic effect on pregnancy.'
%% Cell type:code id: tags:
``` python
```
Chapter 1 Asking LLM for structured data
- Pydantic to give key and type security for dictionaries
- Pydantic to structure LLM output
- function calling for asking LLMs for json output; allows to
better use LLM that are fine tuned for json
- other libraries include Marvin, Langchain, LlamaIndex
Chapter 2 Prompting LLMs
- Schemas to make Prompt engineering modular and reusable
- Literals in schemes seem to outperform enums
- we can extract arbitrary attributes too and limit the amount ???
- can use this extract responses objects as they come with stream
--> complexer requests improved upon their perceived latency
- can use an index list with "good" prompt to extract relationships between entities
- Making parts of the model optional counteracts hallucination!!!
Chapter 3 RAG Applications
- RAG = Retrieval Augmented Generation
-> Retriever, Ranker, and Generator Component
- embedded search = take query/prompt, get information from DB, put info and query into same prompt
-> giving our own data allows model to answer even unseen questions/facts
-> embedded search has limitations, model does not learn over time normally
----> apply query understanding to match backend search capabilities
- use query engine that breaks up query into multiple requests to backend
- from the backend response we can generate summaries
- can use wandb as a library for better documentation/logging
- can use it to decompose questions into multiple and improve results
Chapter 4 Validating LLM Outputs
- use validators to generate feedback for LLM when its response did not confirm to our expectations
- validators check all in parallel
-> field function has lot of stuff already implemented
- can make validation dynamique
--> can use predefined like OpenAI moderation client
-> can also make the llm check whether something is valid
- can use validations to ensure the style of the respons
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment