-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/buhe/langchain-swift
- Loading branch information
Showing
15 changed files
with
1,134 additions
and
745 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 顾艳华 on 2023/11/17. | ||
// | ||
|
||
import Foundation | ||
public class MultiVectorRetriever: BaseRetriever { | ||
let vectorstore: VectorStore | ||
let docstore: BaseStore | ||
let id_key = "doc_id" | ||
|
||
public init(vectorstore: VectorStore, docstore: BaseStore) { | ||
self.vectorstore = vectorstore | ||
self.docstore = docstore | ||
} | ||
|
||
public override func _get_relevant_documents(query: String) async throws -> [Document] { | ||
let sub_docs = await self.vectorstore.similaritySearch(query: query, k: 1) | ||
var ids: [String] = [] | ||
for d in sub_docs { | ||
ids.append(d.metadata[self.id_key]!) | ||
} | ||
let docs = self.docstore.mget(keys: ids) | ||
return docs.map{Document(page_content: $0, metadata: [:])} | ||
} | ||
|
||
// def _get_relevant_documents( | ||
// self, query: str, *, run_manager: CallbackManagerForRetrieverRun | ||
// ) -> List[Document]: | ||
// """Get documents relevant to a query. | ||
// Args: | ||
// query: String to find relevant documents for | ||
// run_manager: The callbacks handler to use | ||
// Returns: | ||
// List of relevant documents | ||
// """ | ||
// sub_docs = self.vectorstore.similarity_search(query, **self.search_kwargs) | ||
// # We do this to maintain the order of the ids that are returned | ||
// ids = [] | ||
// for d in sub_docs: | ||
// if d.metadata[self.id_key] not in ids: | ||
// ids.append(d.metadata[self.id_key]) | ||
// docs = self.docstore.mget(ids) | ||
// return [d for d in docs if d is not None] | ||
} |
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,25 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 顾艳华 on 2023/11/17. | ||
// | ||
|
||
import Foundation | ||
public class BaseStore { | ||
func mget(keys: [String]) -> [String] { | ||
[] | ||
} | ||
|
||
func mset(kvpairs: [(String, String)]) { | ||
|
||
} | ||
|
||
func mdelete(keys: [String]) { | ||
|
||
} | ||
|
||
func keys(prefix: String? = nil) -> [String] { | ||
[] | ||
} | ||
} |
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 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 顾艳华 on 2023/11/17. | ||
// | ||
|
||
import Foundation | ||
public class InMemoryStore: BaseStore { | ||
var store:[String: String] = [:] | ||
public override init() { | ||
super.init() | ||
} | ||
override func mget(keys: [String]) -> [String] { | ||
var values: [String] = [] | ||
for k in keys { | ||
let v = self.store[k] | ||
if v != nil { | ||
values.append(v!) | ||
} | ||
} | ||
return values | ||
} | ||
|
||
override func mset(kvpairs: [(String, String)]) { | ||
for kv in kvpairs { | ||
self.store[kv.0] = kv.1 | ||
} | ||
} | ||
|
||
override func mdelete(keys: [String]) { | ||
for k in keys { | ||
self.store.removeValue(forKey: k) | ||
} | ||
} | ||
|
||
override func keys(prefix: String? = nil) -> [String] { | ||
if prefix == nil { | ||
return Array(self.store.keys) | ||
} else { | ||
var matched: [String] = [] | ||
for k in self.store.keys { | ||
if k.hasPrefix(prefix!) { | ||
matched.append(k) | ||
} | ||
} | ||
return matched | ||
} | ||
} | ||
} |
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
Oops, something went wrong.