Skip to content

Instantly share code, notes, and snippets.

@grafuls
Created March 4, 2024 15:44
Show Gist options
  • Save grafuls/3b96bb0ac304b5eaf45ddb8c9b6985ff to your computer and use it in GitHub Desktop.
Save grafuls/3b96bb0ac304b5eaf45ddb8c9b6985ff to your computer and use it in GitHub Desktop.
Semantic search on MongoDB
#! /usr/bin/env python
from llama_index.core import SummaryIndex
from llama_index.readers.mongodb import SimpleMongoReader
from langchain_community.llms import HuggingFaceEndpoint
import os
os.environ["HUGGINGFACEHUB_API_TOKEN"] = {HUGGINGFACE_API_TOKEN}
host = os.environ["MONGODB_HOST"]
port = 27017
db_name = "quads"
collection_name = "host"
query = {}
field_names = ["name"]
reader = SimpleMongoReader(host, port)
documents = reader.load_data(db_name, collection_name, field_names, query_dict=query)
server_url = os.environ["INFERENCE_URL"]
llm = HuggingFaceEndpoint(
endpoint_url=server_url,
max_new_tokens=512,
top_k=10,
top_p=0.95,
typical_p=0.95,
temperature=0.01,
repetition_penalty=1.03,
)
index = SummaryIndex.from_documents(documents)
query_engine = index.as_query_engine(llm=llm)
response = query_engine.query("how many hosts are there?")
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment