{ "cells": [ { "cell_type": "markdown", "id": "ed47bb62", "metadata": {}, "source": [ "# Hugging Face\n", "Let's load the Hugging Face Embedding class." ] }, { "cell_type": "code", "execution_count": null, "id": "16b20335-da1d-46ba-aa23-fbf3e2c6fe60", "metadata": {}, "outputs": [], "source": [ "!pip install langchain sentence_transformers" ] }, { "cell_type": "code", "execution_count": 2, "id": "861521a9", "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings import HuggingFaceEmbeddings" ] }, { "cell_type": "code", "execution_count": 3, "id": "ff9be586", "metadata": {}, "outputs": [], "source": [ "embeddings = HuggingFaceEmbeddings()" ] }, { "cell_type": "code", "execution_count": 3, "id": "d0a98ae9", "metadata": {}, "outputs": [], "source": [ "text = \"This is a test document.\"" ] }, { "cell_type": "code", "execution_count": 5, "id": "5d6c682b", "metadata": {}, "outputs": [], "source": [ "query_result = embeddings.embed_query(text)" ] }, { "cell_type": "code", "execution_count": 6, "id": "b57b8ce9-ef7d-4e63-979e-aa8763d1f9a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-0.04895168915390968, -0.03986193612217903, -0.021562768146395683]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query_result[:3]" ] }, { "cell_type": "code", "execution_count": 7, "id": "bb5e74c0", "metadata": {}, "outputs": [], "source": [ "doc_result = embeddings.embed_documents([text])" ] }, { "cell_type": "markdown", "id": "92019ef1-5d30-4985-b4e6-c0d98bdfe265", "metadata": {}, "source": [ "## Hugging Face Inference API\n", "We can also access embedding models via the Hugging Face Inference API, which does not require us to install ``sentence_transformers`` and download models locally." ] }, { "cell_type": "code", "execution_count": 1, "id": "66f5c6ba-1446-43e1-b012-800d17cef300", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter your HF Inference API Key:\n", "\n", " ········\n" ] } ], "source": [ "import getpass\n", "\n", "inference_api_key = getpass.getpass(\"Enter your HF Inference API Key:\\n\\n\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "d0623c1f-cd82-4862-9bce-3655cb9b66ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-0.038338541984558105, 0.1234646737575531, -0.028642963618040085]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.embeddings import HuggingFaceInferenceAPIEmbeddings\n", "\n", "embeddings = HuggingFaceInferenceAPIEmbeddings(\n", " api_key=inference_api_key,\n", " model_name=\"sentence-transformers/all-MiniLM-l6-v2\"\n", ")\n", "\n", "query_result = embeddings.embed_query(text)\n", "query_result[:3]" ] } ], "metadata": { "kernelspec": { "display_name": "poetry-venv", "language": "python", "name": "poetry-venv" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.1" }, "vscode": { "interpreter": { "hash": "7377c2ccc78bc62c2683122d48c8cd1fb85a53850a1b1fc29736ed39852c9885" } } }, "nbformat": 4, "nbformat_minor": 5 }