Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Stephen Collins
Stephen Collins

Posted on

     

Introduction to Vector Search and Embeddings

blog post main image

In the age of information, extracting relevant information from massive data sets is crucial. To understand and interpret text, modern natural language processing (NLP) tools employ a technique called vectorization, where textual information is converted into numerical format. In this blog post, we'll explore how vector search and embeddings are used to find similar or relevant texts, and we'll examine a real-world code example.

What Are Embeddings?

Embeddings are the core concept behind converting text into numerical vectors. An embedding is essentially a multi-dimensional representation of a word or a group of words. These numerical representations capture the meaning and semantic relationships between words, allowing computers to "understand" text.

What Is Vector Search?

Vector search refers to finding the closest vectors in a given space that are relevant or similar to a particular query vector. It's like searching for items in a database, but instead of matching text, you're matching mathematical vectors. This search is often conducted using a measure called cosine similarity, which computes the cosine of the angle between two vectors.

Code Example: Finding Relevant Texts with Sentence Embeddings

Let's delve into a Python code example to see these concepts in action. We'll use the Sentence Transformer library to create embeddings and thesklearn library for calculating cosine similarity. We need to use numpy for sorting the results of thecosine_similarity function,openai Python package for making calls to OpenAI's API.

importosfromsentence_transformersimportSentenceTransformerfromsklearn.metrics.pairwiseimportcosine_similarityimportnumpyasnpimportopenaifromdotenvimportload_dotenv
Enter fullscreen modeExit fullscreen mode

Step 1: Setting up the Environment

First, we need to install these packages:

pipinstallsentence-transformers scikit-learn numpy openai python-dotenv
Enter fullscreen modeExit fullscreen mode

Next, we'll initialize our OpenAI API key we need by loading our.env file, suppressing a specific parallelism warning arising from thesentence_transformers package (not a concern for our example script). If you need an OpenAI API key,here's some instructions. For this blog post we are using the Python packagedotenv to load a local.env file for simplicity.

Assuming your.env located right next to your Python script looks like:

OPENAI_API_KEY=YOUR_OPENAI_API_KEY
Enter fullscreen modeExit fullscreen mode

Then we can load our OpenAI API key:

# loads our ".env", assumes it is in the same directory as this Python scriptload_dotenv()# This script is single-threaded,# so it's safe to suppress the TOKENIZERS_PARALLELISM warningos.environ["TOKENIZERS_PARALLELISM"]="true"# Load our OpenAI API keyopenai.api_key=os.getenv('OPENAI_API_KEY')
Enter fullscreen modeExit fullscreen mode

Step 2: Creating Text Embeddings

We use the Sentence Transformer modelbert-base-nli-mean-tokens to convert example texts into numerical vectors (embeddings).

# Initialize our modelmodel=SentenceTransformer('bert-base-nli-mean-tokens')# Example texts for our queries latertexts=["John loves playing basketball on the weekends.","Emily is a huge fan of soccer and never misses a game.","Mike enjoys going golfing with his friends.","Sarah's favorite sport is tennis, and she plays every Thursday.","Tom and his friends are passionate about baseball and often watch games together."]# Create our embeddings. For this blog post, we are just loading in memory.# I'll explain better approaches later in the post.text_vectors=model.encode(texts)
Enter fullscreen modeExit fullscreen mode

Step 3: Defining the Search Function

We define a functionget_relevant_texts to take a query string and find the relevant texts from our corpus.

# perform the vector search to find the relevant textsdefget_relevant_texts(query:str):query_vector=model.encode([query])similarities=cosine_similarity(query_vector,text_vectors)indices=np.argsort(similarities[0])[::-1]returnindices[:2]
Enter fullscreen modeExit fullscreen mode

This function calculates the cosine similarity between the query vector and the text vectors and returns the indices of the two most relevant texts.

Step 4: Using OpenAI's GPT-4 for Contextual Responses

We take the found texts and construct a chat prompt to send to OpenAI's GPT-3.5-turbo model, allowing it to respond with context-aware answers.

# Encapsulate the call to OpenAI gpt-3.5-turbo modeldefget_response_with_context(text:str):relevant_texts_indices=get_relevant_texts(text)relevant_texts=[texts[i]foriinrelevant_texts_indices]content='text:'.join(relevant_texts)+' user:'+textresponse=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role":"system","content":"You are a QA bot given texts to answer questions."},{"role":"user","content":content}],# Temperature set to 0 to reduce the randomness of the response.# Better for applications that expect consistent responses.temperature=0,max_tokens=512)returnresponse.choices[0].message.content
Enter fullscreen modeExit fullscreen mode

Step 5: Testing the Code

We can then test our code with specific queries to see the relevant responses based on the context provided.

test_query="What does John do on the weekend?"answer=get_response_with_context(text=test_query)# Should respond with something like:# "John plays basketball on the weekends."print(answer)# Another exampletest_query="Who likes tennis?"answer=get_response_with_context(text=test_query)# Should respond with something like:# "Sarah likes tennis."print(answer)
Enter fullscreen modeExit fullscreen mode

Next Steps

While in this blog post I've only covered a basic introduction to using embeddings, Here are some next steps to consider:

  1. Vector Databases: As your data grows, efficiently searching through millions of vectors can become a challenge. Specialized vector databases like FAISS, Annoy, or Elasticsearch's vector search capabilities can be explored to manage and search through large-scale vector data. Your sentence is grammatically correct. In addition, databases like SQLite and PostgreSQL have extensions, such assqlite-vss andpgvector, that can be used to store and query vector embeddings, respectively.

  2. More Robust Ways of Creating Embeddings: While the example provided in this blog post offers a straightforward approach to creating embeddings, various other methods and models can be explored. Pre-trained models like BERT, RoBERTa, and DistilBERT offer different characteristics and performance. Investigate these alternatives to find the optimal approach for your specific task, as well as using the OpenAI and Cohere APIs for creating vector embeddings as well.

Conclusion

Vector search and embeddings are powerful tools in modern NLP. They allow us to represent text in a format that machines can interpret, enabling us to find relevant information and derive insights, and provide one way to overcome the current limitations of the context window of large language models. The code example demonstrates how these concepts can be applied using popular libraries to search within a set of texts and obtain contextual answers. Whether it's for a chatbot or a recommendation engine, these techniques can play a vital role in various applications.

Thanks for reading this far! Here's the full example code used in this blog post:

importosfromsentence_transformersimportSentenceTransformerfromsklearn.metrics.pairwiseimportcosine_similarityimportnumpyasnpimportopenaifromdotenvimportload_dotenv# loads our ".env", assumes it is in the same directory as this Python scriptload_dotenv()# This script is single-threaded,# so it's safe to suppress the TOKENIZERS_PARALLELISM warningos.environ["TOKENIZERS_PARALLELISM"]="true"# Initialize our modelmodel=SentenceTransformer('bert-base-nli-mean-tokens')texts=["John loves playing basketball on the weekends.","Emily is a huge fan of soccer and never misses a game.","Mike enjoys going golfing with his friends.","Sarah's favorite sport is tennis, and she plays every Thursday.","Tom and his friends are passionate about baseball and often watch games together."]# Create our embeddings. For this blog post, we are just loading in memory.# I'll explain better approaches later in the post.text_vectors=model.encode(texts)# Load our OpenAI API keyopenai.api_key=os.getenv('OPENAI_API_KEY')# perform the vector search to find the relevant textsdefget_relevant_texts(query:str):query_vector=model.encode([query])similarities=cosine_similarity(query_vector,text_vectors)indices=np.argsort(similarities[0])[::-1]returnindices[:2]# Encapsulate the call to OpenAI gpt-3.5-turbo modeldefget_response_with_context(text:str):relevant_texts_indices=get_relevant_texts(text)relevant_texts=[texts[i]foriinrelevant_texts_indices]content='text:'.join(relevant_texts)+' user:'+textresponse=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role":"system","content":"You are a QA bot given texts to answer questions."},{"role":"user","content":content}],# Temperature set to 0 to reduce the randomness of the response.# Better for applications that expect consistent responsestemperature=0,max_tokens=512)returnresponse.choices[0].message.contenttest_query="What does John do on the weekend?"answer=get_response_with_context(text=test_query)# Should respond with something like:# "John plays basketball on the weekends."print(answer)# Another exampletest_query="Who enjoys tennis?"answer=get_response_with_context(text=test_query)# Should respond with something like:# "Sarah enjoys tennis."print(answer)
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Engineer.Sharing everything I’ve learned in tech.
  • Location
    Austin, Texas
  • Education
    Texas State University
  • Pronouns
    He/Him
  • Work
    Senior Software Engineer
  • Joined

More fromStephen Collins

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp