Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Llama Index
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
Llama Index
Commits
36e25f14
Unverified
Commit
36e25f14
authored
2 years ago
by
Jerry Liu
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
update custom_llms section of docs (#398)
Co-authored-by:
Jerry Liu
<
jerry@robustintelligence.com
>
parent
9a69465e
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/how_to/custom_llms.md
+75
-11
75 additions, 11 deletions
docs/how_to/custom_llms.md
with
75 additions
and
11 deletions
docs/how_to/custom_llms.md
+
75
−
11
View file @
36e25f14
...
@@ -19,28 +19,95 @@ tokens, maximum chunk overlap, and more.
...
@@ -19,28 +19,95 @@ tokens, maximum chunk overlap, and more.
By default, we use OpenAI's
`text-davinci-003`
model. But you may choose to customize
By default, we use OpenAI's
`text-davinci-003`
model. But you may choose to customize
the underlying LLM being used.
the underlying LLM being used.
Below we show a few examples of LLM customization. This includes
-
changing the underlying LLM
-
changing the number of output tokens (for OpenAI, Cohere, or AI21)
-
having more fine-grained control over all parameters for any LLM, from input size to chunk overlap
## Example
## Example: Changing the underlying LLM
An example snippet of customizing the LLM being used is shown below.
An example snippet of customizing the LLM being used is shown below.
In this example, we use
`text-davinci-002`
instead of
`text-davinci-003`
. Note that
In this example, we use
`text-davinci-002`
instead of
`text-davinci-003`
. Note that
you may plug in any LLM shown on Langchain's
you may plug in any LLM shown on Langchain's
[
LLM
](
https://langchain.readthedocs.io/en/latest/modules/llms.html
)
page.
[
LLM
](
https://langchain.readthedocs.io/en/latest/modules/llms.html
)
page.
```
python
```
python
from
gpt_index
import
(
from
gpt_index
import
(
GPTKeywordTableIndex
,
GPTKeywordTableIndex
,
SimpleDirectoryReader
,
SimpleDirectoryReader
,
LLMPredictor
,
LLMPredictor
,
PromptHelper
)
)
from
langchain
import
OpenAI
from
langchain
import
OpenAI
documents
=
SimpleDirectoryReader
(
'
data
'
).
load_data
()
# define LLM
# define LLM
llm_predictor
=
LLMPredictor
(
llm
=
OpenAI
(
temperature
=
0
,
model_name
=
"
text-davinci-002
"
))
llm_predictor
=
LLMPredictor
(
llm
=
OpenAI
(
temperature
=
0
,
model_name
=
"
text-davinci-002
"
))
# build index
index
=
GPTKeywordTableIndex
(
llm_predictor
=
llm_predictor
)
# get response from query
response
=
index
.
query
(
"
What did the author do after his time at Y Combinator?
"
)
```
## Example: Changing the number of output tokens (for OpenAI, Cohere, AI21)
The number of output tokens is usually set to some low number by default (for instance,
with OpenAI the default is 256).
For OpenAI, Cohere, AI21, you just need to set the
`max_tokens`
parameter
(or maxTokens for AI21). We will handle text chunking/calculations under the hood.
```
python
from
gpt_index
import
(
GPTKeywordTableIndex
,
SimpleDirectoryReader
,
LLMPredictor
,
)
from
langchain
import
OpenAI
documents
=
SimpleDirectoryReader
(
'
data
'
).
load_data
()
# define LLM
llm_predictor
=
LLMPredictor
(
llm
=
OpenAI
(
temperature
=
0
,
model_name
=
"
text-davinci-002
"
,
max_tokens
=
512
))
# build index
index
=
GPTKeywordTableIndex
(
llm_predictor
=
llm_predictor
)
# get response from query
response
=
index
.
query
(
"
What did the author do after his time at Y Combinator?
"
)
```
If you are using other LLM classes from langchain, please see below.
## Example: Fine-grained control over all parameters
To have fine-grained control over all parameters, you will need to define
a custom PromptHelper class.
```
python
from
gpt_index
import
(
GPTKeywordTableIndex
,
SimpleDirectoryReader
,
LLMPredictor
,
PromptHelper
)
from
langchain
import
OpenAI
documents
=
SimpleDirectoryReader
(
'
data
'
).
load_data
()
# define prompt helper
# define prompt helper
# set maximum input size
# set maximum input size
max_input_size
=
4096
max_input_size
=
4096
...
@@ -50,16 +117,13 @@ num_output = 256
...
@@ -50,16 +117,13 @@ num_output = 256
max_chunk_overlap
=
20
max_chunk_overlap
=
20
prompt_helper
=
PromptHelper
(
max_input_size
,
num_output
,
max_chunk_overlap
)
prompt_helper
=
PromptHelper
(
max_input_size
,
num_output
,
max_chunk_overlap
)
# load index from disk
# define LLM
index
=
GPTKeywordTableIndex
.
load_from_disk
(
llm_predictor
=
LLMPredictor
(
llm
=
OpenAI
(
temperature
=
0
,
model_name
=
"
text-davinci-002
"
,
max_tokens
=
num_output
))
'
index_table.json
'
,
llm_predictor
=
llm_predictor
,
prompt_helper
=
prompt_helper
)
# build index
index
=
GPTKeywordTableIndex
(
llm_predictor
=
llm_predictor
,
prompt_helper
=
prompt_helper
)
# get response from query
# get response from query
response
=
index
.
query
(
"
What did the author do after his time at Y Combinator?
"
)
response
=
index
.
query
(
"
What did the author do after his time at Y Combinator?
"
)
```
```
In this snippet, the index has already been created and saved to disk. We load
the existing index. We then swap in a new
`LLMPredictor`
and
`PromptHelper`
to set both the new predictor as well as the new prompt parameters.
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment