Skip to main content

Deepglint fse vectorstore

Project description

Discord ZH doc License

A fse vectorstore for langchain

fsevector is a vectorstore python library for langchain based on fse and postgres. it provides vector storage function, vector retrieval.

One of the most common ways to store and search over unstructured data is to embed it and store the resulting embedding vectors, and then at query time to embed the unstructured query and retrieve the embedding vectors that are 'most similar' to the embedded query. A vector store takes care of storing embedded data and performing vector search for you.

Installation

Deploy postgres and fse

Before using fsevector, you need to deploy postgres and fse services (it is recommended to install the DeepEngine of deepglint).

Trial environment

call interface with fsedoc_connection_string and fseaddr_connection_string
fsedoc_connection_string="fsedoc://fsevector:test1234@192.168.3.111:24049"
fseaddr_connection_string="fseaddr://192.168.3.111:3154"

or add the following environment variables
export FSEDOC_CONNECTION_STRING="fsedoc://fsevector:test1234@192.168.3.111:24049"
export FSEADDR_CONNECTION_STRING="fseaddr://192.168.3.111:3154"

Install fsevector

pip install fsevector

Documentation

More information can be found on the examples

Usage

Instructions for use

When using fsevector, you need the following steps:
1. Enter fsedoc_connection_string and fseaddr_connection_string in the following call example or add environment variables.
2. Add OpenAI-related environment variables.

Example

import openai
from langchain.chat_models import AzureChatOpenAI
from langchain.chains import RetrievalQAWithSourcesChain
from langchain.embeddings import OpenAIEmbeddings
from langchain.chat_models import AzureChatOpenAI
from langchain.schema import Document

from fsevector.fsevector import FseVector

openai.api_key = os.environ["OPENAI_API_KEY"]
openai.api_base = os.environ["OPENAI_API_BASE"]
openai.api_version = os.environ["OPENAI_API_VERSION"]
openai.api_type = os.environ["OPENAI_API_TYPE"]

#init fseVector
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
fseVector = FseVector(fsedoc_connection_string="fsedoc://username:passwd@ip:port",
                      fseaddr_connection_string="fseaddr://ip:port",
                      embedding_function=embeddings,
                      collection_name="knowledge_test")

#init chain
DEPLOYMENT_NAME = "gpt-35-turbo"  # gpt-35-turbo gpt-35-turbo-16k
llm = AzureChatOpenAI(deployment_name=DEPLOYMENT_NAME)
chain = RetrievalQAWithSourcesChain.from_chain_type(llm=llm,
                                chain_type="stuff", verbose=False, memory=None,
                                retriever=fseVector.as_retriever(search_type='similarity_score_threshold',
                                                                search_kwargs={'score_threshold': 0.3, 'k': 3}),
                                return_source_documents=False)

#add doc or texts
fseVector.add_texts(texts=['trigger phrases: "get emails", "send emails", "send an email", "email"\n## (Mac) Get emails\nExecute the following AppleScript command to get the content of the last X (in this case, 3) emails from the Mail application:\ntell application "Mail" to get content of messages 1 through 3 of inbox\n## (Mac) Send emails\nUse Applescript.'], metadatas=[{"source": "email.txt", "key_list": ["emails", "get emails"]}])  

#retrieval
result = chain({"question": 'get emails'})
output = f"Answer: {result['answer']}\nSources: {result['sources']}\nresult: {result}"
print(output)        

Other init fsevector methods

#from_index
embeddings = OpenAIEmbeddings()
fseVector = FseVector.from_index(
                    fsedoc_connection_string="fsedoc://username:passwd@ip:port",
                    fseaddr_connection_string="fseaddr://ip:port",
                    collection_name="knowledge_test",
                    embedding=embeddings)

#from_texts
embeddings = OpenAIEmbeddings()
fseVector = FseVector.from_texts(
                    fsedoc_connection_string="fsedoc://username:passwd@ip:port",
                    fseaddr_connection_string="fseaddr://ip:port",
                    collection_name="knowledge_test",
                    texts=["teststssss"],
                    embedding=embeddings,
                    metadatas=[{"source": "teststssss"}],
                    ids=[str(11111)],
                    pre_delete_collection=True)

#from_documents
ids=[11]
embeddings = OpenAIEmbeddings()
doc=[Document(page_content="xxxx", metadata={"source": "teststssss","key_list":["emails", "get emails"]})]
fseVector = FseVector.from_documents(
                    fsedoc_connection_string="fsedoc://username:passwd@ip:port",
                    fseaddr_connection_string="fseaddr://ip:port",
                    collection_name="knowledge_test",
                    documents=doc,
                    embedding=embeddings, ids=ids)

#from_embeddings
embeddings = OpenAIEmbeddings()
text_embeddings = embeddings.embed_documents(texts)
text_embedding_pairs = list(zip(texts, text_embeddings))
fseVector = FseVector.from_embeddings(
                    fsedoc_connection_string="fsedoc://username:passwd@ip:port",
                    fseaddr_connection_string="fseaddr://ip:port",
                    collection_name="knowledge_test",
                    text_embeddings=text_embedding_pairs,
                    embedding=embeddings)

Supported interface

from_documents
from_texts
from_embeddings
from_index
add_embeddings
add_texts
similarity_search
similarity_search_with_score
similarity_search_with_score_by_vector
similarity_search_by_vector

Development

  • CI pipeline

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fsevector-1.2.1.tar.gz (16.6 kB view details)

Uploaded Source

File details

Details for the file fsevector-1.2.1.tar.gz.

File metadata

  • Download URL: fsevector-1.2.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for fsevector-1.2.1.tar.gz
Algorithm Hash digest
SHA256 51b89dc41ed0c8b14cc1b782e953d5273e2d1d14c9ef0f1541c92289a292e8b5
MD5 a93faac965786e4f30142b75013ee44a
BLAKE2b-256 09478f614128dce01f2f82e2950653615d1c11d764c8ddf23b011c845ff9097c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page