-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added mm-rag example and started the response synthesis for it
- Loading branch information
1 parent
76f7256
commit f4b0961
Showing
16 changed files
with
219 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
MultiModalResponseSynthesizer, | ||
OpenAI, | ||
ServiceContext, | ||
VectorStoreIndex, | ||
serviceContextFromDefaults, | ||
storageContextFromDefaults, | ||
} from "llamaindex"; | ||
|
||
export async function createIndex(serviceContext: ServiceContext) { | ||
// set up vector store index with two vector stores, one for text, the other for images | ||
const storageContext = await storageContextFromDefaults({ | ||
persistDir: "storage", | ||
storeImages: true, | ||
}); | ||
return await VectorStoreIndex.init({ | ||
nodes: [], | ||
storageContext, | ||
serviceContext, | ||
}); | ||
} | ||
|
||
async function main() { | ||
const llm = new OpenAI({ model: "gpt-4-vision-preview", maxTokens: 512 }); | ||
const serviceContext = serviceContextFromDefaults({ | ||
llm, | ||
chunkSize: 512, | ||
chunkOverlap: 20, | ||
}); | ||
const index = await createIndex(serviceContext); | ||
|
||
const queryEngine = index.asQueryEngine({ | ||
responseSynthesizer: new MultiModalResponseSynthesizer({ serviceContext }), | ||
// TODO: set imageSimilarityTopK: 1, | ||
retriever: index.asRetriever({ similarityTopK: 2 }), | ||
}); | ||
const result = await queryEngine.query( | ||
"what are Vincent van Gogh's famous paintings", | ||
); | ||
console.log(result.response); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/core/src/synthesizers/MultiModalResponseSynthesizer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { MessageContentDetail } from "../ChatEngine"; | ||
import { MetadataMode, NodeWithScore, splitNodesByType } from "../Node"; | ||
import { Response } from "../Response"; | ||
import { ServiceContext, serviceContextFromDefaults } from "../ServiceContext"; | ||
import { Event } from "../callbacks/CallbackManager"; | ||
import { TextQaPrompt, defaultTextQaPrompt } from "./../Prompt"; | ||
import { BaseSynthesizer } from "./types"; | ||
|
||
export class MultiModalResponseSynthesizer implements BaseSynthesizer { | ||
serviceContext: ServiceContext; | ||
metadataMode: MetadataMode; | ||
textQATemplate: TextQaPrompt; | ||
|
||
constructor({ | ||
serviceContext, | ||
textQATemplate, | ||
metadataMode, | ||
}: Partial<MultiModalResponseSynthesizer> = {}) { | ||
this.serviceContext = serviceContext ?? serviceContextFromDefaults(); | ||
this.metadataMode = metadataMode ?? MetadataMode.NONE; | ||
this.textQATemplate = textQATemplate ?? defaultTextQaPrompt; | ||
} | ||
|
||
async synthesize( | ||
query: string, | ||
nodesWithScore: NodeWithScore[], | ||
parentEvent?: Event, | ||
): Promise<Response> { | ||
const nodes = nodesWithScore.map(({ node }) => node); | ||
const { imageNodes, textNodes } = splitNodesByType(nodes); | ||
const textChunks = textNodes.map((node) => | ||
node.getContent(this.metadataMode), | ||
); | ||
// TODO: use builders to generate context | ||
const context = textChunks.join("\n\n"); | ||
const textPrompt = this.textQATemplate({ context, query }); | ||
// TODO: get images from imageNodes | ||
const prompt: MessageContentDetail[] = [ | ||
{ type: "text", text: textPrompt }, | ||
{ | ||
type: "image_url", | ||
image_url: { | ||
url: "https://upload.wikimedia.org/wikipedia/commons/b/b0/Vincent_van_Gogh_%281853-1890%29_Caf%C3%A9terras_bij_nacht_%28place_du_Forum%29_Kr%C3%B6ller-M%C3%BCller_Museum_Otterlo_23-8-2016_13-35-40.JPG", | ||
}, | ||
}, | ||
]; | ||
let response = await this.serviceContext.llm.complete(prompt, parentEvent); | ||
return new Response(response.message.content, nodes); | ||
} | ||
} |
Oops, something went wrong.