Skip to content

Instantly share code, notes, and snippets.

@dudanogueira
Last active February 16, 2024 12:59
Show Gist options
  • Save dudanogueira/9660de2f9bb68151afd741d3c4096b5e to your computer and use it in GitHub Desktop.
Save dudanogueira/9660de2f9bb68151afd741d3c4096b5e to your computer and use it in GitHub Desktop.
ReltiveScoreFusion
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import weaviate\n",
"client = weaviate.connect_to_local()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from weaviate import classes as wvc\n",
"\n",
"client.collections.delete(\"Hybrid\")\n",
"col = client.collections.create(\n",
" name=\"Hybrid\",\n",
" vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),\n",
" properties=[\n",
" wvc.config.Property(\n",
" name=\"text\",\n",
" data_type=wvc.config.DataType.TEXT,\n",
" description=\"The text\",\n",
" ),\n",
" ],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"UUID('ca7a66fa-1182-40c4-95c9-f280653a7c1e')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# lets add object\n",
"col.data.insert({'text': 'Some text about soccer'})\n",
"col.data.insert({'text': 'Some text about Pelé'})\n",
"col.data.insert({'text': 'Some text about Goalkeeper'})"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'text': 'Some text about soccer'}\n",
"{'text': 'Some text about Goalkeeper'}\n",
"{'text': 'Some text about Pelé'}\n"
]
}
],
"source": [
"from weaviate import classes as wvc\n",
"col = client.collections.get(\"Hybrid\")\n",
"q = col.query.hybrid(\n",
" query=\"soccer\",\n",
" fusion_type=wvc.query.HybridFusion.RELATIVE_SCORE,\n",
" alpha=0.5\n",
")\n",
"for r in q.objects:\n",
" print(r.properties)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/homebrew/lib/python3.11/site-packages/weaviate/warnings.py:158: DeprecationWarning: Dep016: You are using the Weaviate v3 client, which is deprecated.\n",
" Consider upgrading to the new and improved v4 client instead!\n",
" See here for usage: https://weaviate.io/developers/weaviate/client-libraries/python\n",
" \n",
" warnings.warn(\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# testing with client 3\n",
"client3 = weaviate.Client(\"http://localhost:8080\")\n",
"client3.is_ready()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"#getting a vector\n",
"# Now lets add the vectors ourself\n",
"import os\n",
"import requests\n",
"\n",
"headers = {\n",
" 'Content-Type': 'application/json',\n",
" 'Authorization': 'Bearer ' + os.getenv('OPENAI_API_KEY', ''),\n",
"}\n",
"\n",
"json_data = {\n",
" 'input': 'Take the ball with hands',\n",
" 'model': 'text-embedding-ada-002',\n",
"}\n",
"\n",
"response = requests.post('https://api.openai.com/v1/embeddings', headers=headers, json=json_data)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"vector = response.json()[\"data\"][0][\"embedding\"]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'data': {'Get': {'Hybrid': [{'text': 'Some text about soccer'},\n",
" {'text': 'Some text about Goalkeeper'},\n",
" {'text': 'Some text about Pelé'}]}}}"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from weaviate.gql.get import HybridFusion\n",
"client3.query.get(\"Hybrid\", \"text\").with_hybrid(query=\"soccer\", alpha=0.5, vector=vector, fusion_type=HybridFusion.RELATIVE_SCORE).do()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment