Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
add embeddings with openai
Marco Vinciguerra6124fbdfca9d
diff --git a/scrapegraphai/nodes/generate_answer_node_k_level.py b/scrapegraphai/nodes/generate_answer_node_k_level.pyindex 10977617..8dea5c98 100644--- a/scrapegraphai/nodes/generate_answer_node_k_level.py+++ b/scrapegraphai/nodes/generate_answer_node_k_level.py@@ -97,10 +97,24 @@ class GenerateAnswerNodeKLevel(BaseNode):client = state["vectorial_db"]- answer_db = client.query(- collection_name="vectorial_collection",- query_text=state["question"]+ if state.get("embeddings"):+ import openai+ openai_client = openai.Client()++ answer_db = client.search(+ collection_name="collection",+ query_vector=openai_client.embeddings.create(+ input=["What is the best to use for vector search scaling?"],+ model=state.get("embeddings").get("model"),+ )+ .data[0]+ .embedding,)+ else:+ answer_db = client.query(+ collection_name="vectorial_collection",+ query_text=state["question"]+ )## TODO: from the id get the dataresults_db = [elem for elem in state[answer_db]]diff --git a/scrapegraphai/nodes/rag_node.py b/scrapegraphai/nodes/rag_node.pyindex cac41a99..3f861478 100644--- a/scrapegraphai/nodes/rag_node.py+++ b/scrapegraphai/nodes/rag_node.py@@ -4,6 +4,7 @@ RAGNode Modulefrom typing import List, Optionalfrom .base_node import BaseNodefrom qdrant_client import QdrantClient+from qdrant_client.models import PointStruct, VectorParams, Distanceclass RAGNode(BaseNode):"""@@ -52,6 +53,41 @@ class RAGNode(BaseNode):docs = [elem.get("summary") for elem in state.get("descriptions", {})]ids = [elem.get("id") for elem in state.get("descriptions", {})]+ if state.get("embeddings"):+ import openai+ openai_client = openai.Client()++ files = state.get("documents")++ array_of_embeddings = []+ i=0++ for file in files:+ embeddings = openai_client.embeddings.create(input=file,+ model=state.get("embeddings").get("model"))+ i+=1+ points = PointStruct(+ id=i,+ vector=embeddings,+ payload={"text": file},+ )++ array_of_embeddings.append(points)++ collection_name = "collection"++ client.create_collection(+ collection_name,+ vectors_config=VectorParams(+ size=1536,+ distance=Distance.COSINE,+ ),+ )+ client.upsert(collection_name, points)++ state["vectorial_db"] = client+ return state+client.add(collection_name="vectorial_collection",documents=docs,