Skip to content

Instantly share code, notes, and snippets.

@muyiwexy
Created November 28, 2023 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muyiwexy/631fbe15862215a55698a0e5be19f794 to your computer and use it in GitHub Desktop.
Save muyiwexy/631fbe15862215a55698a0e5be19f794 to your computer and use it in GitHub Desktop.
langchain_service_impl.dart(4)
class LangchainServicesImpl extends LangchainService {
final Connection connection;
final OpenAIEmbeddings embeddings;
final OpenAI openAI;
LangchainServicesImpl({
required this.connection,
required this.embeddings,
required this.openAI,
});
// do something
// do something
// do something
// do something
// do something
// do something
@override
Future<void> storeDocumentData(Document doc, List<Document> chunks,
List<List<double>> embeddedDoc, String tableName) async {
debugPrint("Storing chunks and emedded vectors in the $tableName table");
await connection.runTx((s) async {
for (int i = 0; i < chunks.length; i++) {
final txtPath = doc.metadata['source'] as String;
final chunk = chunks[i];
final embeddingArray = embeddedDoc[i];
await s.execute(
Sql.named(
'INSERT INTO $tableName (id, metadata, embedding) VALUES (@id, @metadata, @embedding)',
),
parameters: {
'id': '${txtPath}_$i',
'metadata': {
...chunk.metadata,
'loc': jsonEncode(chunk.metadata['loc']),
'pageContent': chunk.pageContent,
'txtPath': txtPath,
},
'embedding': '$embeddingArray',
},
);
}
});
}
}
void debugPrint(String message) {
if (kDebugMode) {
print(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment