forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.4 KiB
Plaintext
137 lines
3.4 KiB
Plaintext
{
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0,
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"name": "python3",
|
|
"display_name": "Python 3"
|
|
},
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"!pip install elasticsearch langchain"
|
|
],
|
|
"metadata": {
|
|
"id": "OOiBBjc0Kd-6"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"%env ES_CLOUDID=<cloud id from cloud.elastic.co>\n",
|
|
"%env ES_USER=<user>\n",
|
|
"%env ES_PASS=<password>\n",
|
|
"\n",
|
|
"es_cloudid = os.environ.get(\"ES_CLOUDID\")\n",
|
|
"es_user = os.environ.get(\"ES_USER\")\n",
|
|
"es_pass = os.environ.get(\"ES_PASS\")"
|
|
],
|
|
"metadata": {
|
|
"id": "Wr8unljAKdCh"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Connect to Elasticsearch\n",
|
|
"es_connection = Elasticsearch(cloud_id=es_cloudid, basic_auth=(es_user, es_pass))"
|
|
],
|
|
"metadata": {
|
|
"id": "YIDsrBqTKs85"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Define the model ID and input field name (if different from default)\n",
|
|
"model_id = \"your_model_id\"\n",
|
|
"input_field = \"your_input_field\" # Optional, only if different from 'text_field'"
|
|
],
|
|
"metadata": {
|
|
"id": "sfFhnFHOKvbM"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Initialize the ElasticsearchEmbeddings instance\n",
|
|
"embeddings_generator = ElasticsearchEmbeddings(es_connection, model_id, input_field)"
|
|
],
|
|
"metadata": {
|
|
"id": "V-pCgqLCKvYs"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Generate embeddings for a list of documents\n",
|
|
"documents = [\n",
|
|
" \"This is an example document.\",\n",
|
|
" \"Another example document to generate embeddings for.\",\n",
|
|
" ]\n",
|
|
"document_embeddings = embeddings_generator.embed_documents(documents)"
|
|
],
|
|
"metadata": {
|
|
"id": "lJg2iRDWKvV_"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Print the generated document embeddings\n",
|
|
"for i, doc_embedding in enumerate(document_embeddings):\n",
|
|
" print(f\"Embedding for document {i + 1}: {doc_embedding}\")"
|
|
],
|
|
"metadata": {
|
|
"id": "R3sYQlh3KvTQ"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Generate an embedding for a single query text\n",
|
|
"query_text = \"What is the meaning of life?\"\n",
|
|
"query_embedding = embeddings_generator.embed_query(query_text)"
|
|
],
|
|
"metadata": {
|
|
"id": "n0un5Vc0KvQd"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Print the generated query embedding\n",
|
|
"print(f\"Embedding for query: {query_embedding}\")"
|
|
],
|
|
"metadata": {
|
|
"id": "PANph6pmKvLD"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
}
|
|
]
|
|
} |