Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LlamaIndexTS
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
LlamaIndexTS
Commits
b87e6d9c
Commit
b87e6d9c
authored
1 year ago
by
Sourabh Desai
Browse files
Options
Downloads
Patches
Plain Diff
finish implementation for llm list index retriever
parent
8d618a6b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/core/src/index/list/ListIndexRetriever.ts
+13
-14
13 additions, 14 deletions
packages/core/src/index/list/ListIndexRetriever.ts
packages/core/src/index/list/utils.ts
+72
-2
72 additions, 2 deletions
packages/core/src/index/list/utils.ts
with
85 additions
and
16 deletions
packages/core/src/index/list/ListIndexRetriever.ts
+
13
−
14
View file @
b87e6d9c
...
@@ -3,10 +3,13 @@ import { NodeWithScore } from "../../Node";
...
@@ -3,10 +3,13 @@ import { NodeWithScore } from "../../Node";
import
{
ListIndex
}
from
"
./ListIndex
"
;
import
{
ListIndex
}
from
"
./ListIndex
"
;
import
{
ServiceContext
}
from
"
../../ServiceContext
"
;
import
{
ServiceContext
}
from
"
../../ServiceContext
"
;
import
{
import
{
NodeFormatterFunction
,
ChoiceSelectParserFunction
,
defaultFormatNodeBatchFn
,
defaultFormatNodeBatchFn
,
defaultParseChoiceSelectAnswerFn
,
defaultParseChoiceSelectAnswerFn
,
}
from
"
./utils
"
;
}
from
"
./utils
"
;
import
{
SimplePrompt
,
defaultChoiceSelectPrompt
}
from
"
../../Prompt
"
;
import
{
SimplePrompt
,
defaultChoiceSelectPrompt
}
from
"
../../Prompt
"
;
import
_
from
"
lodash
"
;
/**
/**
* Simple retriever for ListIndex that returns all nodes
* Simple retriever for ListIndex that returns all nodes
...
@@ -34,16 +37,16 @@ export class ListIndexLLMRetriever implements BaseRetriever {
...
@@ -34,16 +37,16 @@ export class ListIndexLLMRetriever implements BaseRetriever {
index
:
ListIndex
;
index
:
ListIndex
;
choiceSelectPrompt
:
SimplePrompt
;
choiceSelectPrompt
:
SimplePrompt
;
choiceBatchSize
:
number
;
choiceBatchSize
:
number
;
formatNodeBatchFn
:
Function
;
formatNodeBatchFn
:
NodeFormatter
Function
;
parseChoiceSelectAnswerFn
:
Function
;
parseChoiceSelectAnswerFn
:
ChoiceSelectParser
Function
;
serviceContext
:
ServiceContext
;
serviceContext
:
ServiceContext
;
constructor
(
constructor
(
index
:
ListIndex
,
index
:
ListIndex
,
choiceSelectPrompt
?:
SimplePrompt
,
choiceSelectPrompt
?:
SimplePrompt
,
choiceBatchSize
:
number
=
10
,
choiceBatchSize
:
number
=
10
,
formatNodeBatchFn
?:
Function
,
formatNodeBatchFn
?:
NodeFormatter
Function
,
parseChoiceSelectAnswerFn
?:
Function
,
parseChoiceSelectAnswerFn
?:
ChoiceSelectParser
Function
,
serviceContext
?:
ServiceContext
serviceContext
?:
ServiceContext
)
{
)
{
this
.
index
=
index
;
this
.
index
=
index
;
...
@@ -70,23 +73,19 @@ export class ListIndexLLMRetriever implements BaseRetriever {
...
@@ -70,23 +73,19 @@ export class ListIndexLLMRetriever implements BaseRetriever {
input
input
);
);
const
[
rawChoices
,
relevances
]
=
this
.
parseChoiceSelectAnswerFn
(
// parseResult is a map from doc number to relevance score
const
parseResult
=
this
.
parseChoiceSelectAnswerFn
(
rawResponse
,
rawResponse
,
nodesBatch
.
length
nodesBatch
.
length
);
);
const
choiceIndexes
=
rawChoices
.
map
(
const
choiceNodeIds
=
nodeIdsBatch
.
filter
((
nodeId
,
idx
)
=>
{
(
choice
:
string
)
=>
parseInt
(
choice
)
-
1
return
`
${
idx
}
`
in
parseResult
;
);
});
const
choiceNodeIds
=
choiceIndexes
.
map
(
(
idx
:
number
)
=>
nodeIdsBatch
[
idx
]
);
const
choiceNodes
=
await
this
.
index
.
docStore
.
getNodes
(
choiceNodeIds
);
const
choiceNodes
=
await
this
.
index
.
docStore
.
getNodes
(
choiceNodeIds
);
const
relevancesFilled
=
relevances
||
new
Array
(
choiceNodes
.
length
).
fill
(
1.0
);
const
nodeWithScores
=
choiceNodes
.
map
((
node
,
i
)
=>
({
const
nodeWithScores
=
choiceNodes
.
map
((
node
,
i
)
=>
({
node
:
node
,
node
:
node
,
score
:
relevancesFilled
[
i
]
,
score
:
_
.
get
(
parseResult
,
`
${
i
+
1
}
`
,
1
)
,
}));
}));
results
.
push
(...
nodeWithScores
);
results
.
push
(...
nodeWithScores
);
...
...
This diff is collapsed.
Click to expand it.
packages/core/src/index/list/utils.ts
+
72
−
2
View file @
b87e6d9c
export
function
defaultFormatNodeBatchFn
()
{}
import
{
BaseNode
,
MetadataMode
}
from
"
../../Node
"
;
import
_
from
"
lodash
"
;
export
function
defaultParseChoiceSelectAnswerFn
()
{}
export
type
NodeFormatterFunction
=
(
summaryNodes
:
BaseNode
[])
=>
string
;
export
const
defaultFormatNodeBatchFn
:
NodeFormatterFunction
=
(
summaryNodes
:
BaseNode
[]
):
string
=>
{
return
summaryNodes
.
map
((
node
,
idx
)
=>
{
return
`
Document
${
idx
+
1
}
:
${
node
.
getContent
(
MetadataMode
.
LLM
)}
`
.
trim
();
})
.
join
(
"
\n\n
"
);
};
// map from document number to its relevance score
export
type
ChoiceSelectParseResult
=
{
[
docNumber
:
number
]:
number
};
export
type
ChoiceSelectParserFunction
=
(
answer
:
string
,
numChoices
:
number
,
raiseErr
?:
boolean
)
=>
ChoiceSelectParseResult
;
export
const
defaultParseChoiceSelectAnswerFn
:
ChoiceSelectParserFunction
=
(
answer
:
string
,
numChoices
:
number
,
raiseErr
:
boolean
=
false
):
ChoiceSelectParseResult
=>
{
// split the line into the answer number and relevance score portions
const
lineTokens
:
string
[][]
=
answer
.
split
(
"
\n
"
)
.
map
((
line
:
string
)
=>
{
let
lineTokens
=
line
.
split
(
"
,
"
);
if
(
lineTokens
.
length
!==
2
)
{
if
(
raiseErr
)
{
throw
new
Error
(
`Invalid answer line:
${
line
}
. Answer line must be of the form: answer_num: <int>, answer_relevance: <float>`
);
}
else
{
return
null
;
}
}
return
lineTokens
;
})
.
filter
((
lineTokens
)
=>
!
_
.
isNil
(
lineTokens
))
as
string
[][];
// parse the answer number and relevance score
return
lineTokens
.
reduce
(
(
parseResult
:
ChoiceSelectParseResult
,
lineToken
:
string
[])
=>
{
try
{
let
docNum
=
parseInt
(
lineToken
[
0
].
split
(
"
:
"
)[
1
].
trim
());
let
answerRelevance
=
parseFloat
(
lineToken
[
1
].
split
(
"
:
"
)[
1
].
trim
());
if
(
docNum
<
1
||
docNum
>
numChoices
)
{
if
(
raiseErr
)
{
throw
new
Error
(
`Invalid answer number:
${
docNum
}
. Answer number must be between 1 and
${
numChoices
}
`
);
}
else
{
parseResult
[
docNum
]
=
answerRelevance
;
}
}
}
catch
(
e
)
{
if
(
raiseErr
)
{
throw
e
;
}
}
return
parseResult
;
},
{}
);
};
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