Package: onecontext
OneContextTable of ContentsLLM Context as a ServiceQuick StartLicenseLLM Context as a ServiceOneContext makes it really easy and fast to augment your LLM application with your own data
in a few API calls. Upload your data to aKnowledge Baseand directly it
query with natural languge to retrieve relevant context for your LLM application.We manage the full document processing and retrieval pipeline so that you don't have to:document ingestion, chunking and cleaningeffcient vector embeddings at scale using state of the art open source modelslow latency multi stage query pipeline to provide the most relevant context
for your LLM applicationWe keep up with the latest research to provide an accurate and fast retrieval pipeline
based on model evalution and best practice heuristics.Multi stage query pipeline out of the box:Fast base-model retrieves a large pool of documentsCross-encoder reranks the retrieved documents to provide the precise
results relevant to the query.Use Cases:Question Answering over a large knowledge baseLong term memorry for chatbotsRuntime context for instruction following agentsPrevent and detect hallucinations based on custom dataQuick Startpip install onecontextConfigurationexport ONECONTEXT_API_KEY="YOUR_API_KEY"You can get an api key by joining our closed beta. Email Ross atross@onecontext.aito get on the list.UsageCreate a Knowledge Base:fromonecontextimportKnowledgeBasemy_knowledge_base=KnowledgeBase("my_knowledge_base")my_knowledge_base.create()List Knowledge Basesfromonecontextimportlist_knowledge_basesprint(list_knowledge_bases())Upload files to the Knowledge Base:You can upload an entire directory like this:my_kb=KnowledgeBase("my_knowledge_base")directory="/path/to/local_directory"my_kb.upload_from_directory(directory)Or, you can upload an individual file like this:my_kb=KnowledgeBase("my_knowledge_base")my_kb.upload_file("/path/to/local_file.pdf")If you like, you can also add metadata to your files. This makes it really easy to filter your query-space later on. Metadata can be any key-value pairs, passed as a dictionary. For example:my_kb=KnowledgeBase("my_knowledge_base")my_kb.upload_file("/path/to/local_file.pdf",metadata={"ContainsPII":True,"author":"ross","description":"passport","file-type":"scan","category":"personal"})Currently, you can upload any of [.pdf, .docx, .txt] files. Don't worry if the PDF is a scan (and doesn't have easily extractable text), OneContext will figure it out via OCR.
In the near future you'll be able to upload video, audio, and connect to multiple file-storage platforms.Once the files have been uploaded they will be processed, chunked
and embedded by OneContext.Check sync status:print(my_knowledge_base.is_synced)Query the Knowledge BasefromonecontextimportRetrieverretriever=Retriever(knowledge_bases=[my_kb])documents=retriever.query("what is onecontext?",output_k=20)And, filtering by metadata:fromonecontextimportRetrieverretriever=Retriever(knowledge_bases=[my_kb])documents=retriever.query("what is onecontext?",output_k=20,metadata_filters={"ContainsPII":True,"author":"ross"})By default the query pipeline is composed of two steps:Retrieval: fetch the larger pool of documents (rerank_pool_size)Re-ranking: re-rank the results with a downstream model to get most relevant documentsTo improve recall you can increase the rerank_pool_size:documents=retriever.query("what is onecontext?",output_k=10,rerank_pool_size=80)You can also skip the re-ranking step entirely if you want to prioritise speed over accuracy of results.documents=retriever.query_no_rerank("what is onecontext?")Licenseonecontextis distributed under the terms of theMITlicense.