Kindly note
- US proxy server is required when calling OpenAI models.
- Users should have an API key from the OpenAI website and start a Hippo instance before using models.
- The dimensions output by OpenAI's Text-embedding-ada-002 model are 1536. Please kindly note when specifying column dimensions during table creation.
- Vector columns can only be queried when they have index.
- At least Python 3.8 is required when using OpenAI models.
Install Dependency
Initially, the installation of certain dependencies, such as OpenAI, Langchain, and Hippo-API are required. Please note that users should install the appropriate versions tailored to their environment.
!pip install langchain=0.1.13
!pip install tiktoken openai
!pip install hippo-api
Requirement already satisfied: hippo-api==1.1.0.rc3 in /Users/daochengzhang/miniforge3/envs/py310/lib/python3.10/site-packages (1.1.0rc3)
Requirement already satisfied: pyyaml>=6.0 in /Users/daochengzhang/miniforge3/envs/py310/lib/python3.10/site-packages (from hippo-api==1.1.0.rc3) (6.0.1)
Import Dependency Package
import os
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores.hippo import Hippo
Load Knowledge Document
# Configure usersβ own API Key
OPENAI_API_KEY = "your_key"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
# Configure usersβ proxy
os.environ['http_proxy'] = 'your_proxy'
# Load local file
loader = TextLoader("../../modules/state_of_the_union.txt")
documents = loader.load()
Segment Knowledge Document
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
Declare Embedding Model
Users can create the OpenAI or Azure embedding model using the OpenAIEmbeddings method from Langchain.
# OpenAI
embeddings = OpenAIEmbeddings()
# Azure
# embeddings = OpenAIEmbeddings(
# openai_api_type="azure",
# openai_api_base="x x x",
# openai_api_version="x x x",
# model="x x x",
# deployment="x x x",
# openai_api_key="x x x"
# )
Declare Hippo Client
HIPPO_CONNECTION = {"host": "IP", "port": "PORT"}
Store Document
print("input...")
# Insert Docs
vector_store = Hippo.from_documents(
docs,
embedding=embeddings,
table_name="langchain_test",
connection_args=HIPPO_CONNECTION,
)
print("success")
input...
success
Conduct Knowledge-based Question and Answer
Create a Large Language Question-Answering Model
Users can create the OpenAI or Azure large language question-answering model respectively using the AzureChatOpenAI and ChatOpenAI methods from Langchain.
# llm = AzureChatOpenAI(
# openai_api_base="x x x",
# openai_api_version="xxx",
# deployment_name="xxx",
# openai_api_key="xxx",
# openai_api_type="azure"
# )
llm = ChatOpenAI(openai_api_key="YOUR OPENAI KEY", model_name="gpt-3.5-turbo-16k")
Acquire Related Knowledge Based on the Question
query = "Please introduce COVID-19"
# query = "Please introduce Hippo Core Architecture"
# query = "What operations does the Hippo Vector Database support for vector data?"
# query = "Does Hippo use hardware acceleration technology? Briefly introduce hardware acceleration technology."
# Retrieve similar content from the knowledge base, then fetch the top two most similar texts.
res = vector_store.similarity_search(query, 2)
content_list = [item.page_content for item in res]
text = "".join(content_list)
Construct Prompt Template
prompt = f"""
Please use the content of the following [Article] to answer my question. If you don't know, please say you don't know, and the answer should be concise."
[Article]:{text}
Please answer this question in conjunction with the above article:{query}
"""
Wait for Large Language Model to Generate an Answer
response_with_hippo = llm.predict(prompt)
print(f"response_with_hippo:{response_with_hippo}")
response = llm.predict(query)
print("==========================================")
print(f"response_without_hippo:{response}")
response_with_hippo:COVID-19 is a virus that has impacted every aspect of our lives for over two years. It is a highly contagious and mutates easily, requiring us to remain vigilant in combating its spread. However, due to progress made and the resilience of individuals, we are now able to move forward safely and return to more normal routines.
==========================================
response_without_hippo:COVID-19 is a contagious respiratory illness caused by the novel coronavirus SARS-CoV-2. It was first identified in December 2019 in Wuhan, China and has since spread globally, leading to a pandemic. The virus primarily spreads through respiratory droplets when an infected person coughs, sneezes, talks, or breathes, and can also spread by touching contaminated surfaces and then touching the face. COVID-19 symptoms include fever, cough, shortness of breath, fatigue, muscle or body aches, sore throat, loss of taste or smell, headache, and in severe cases, pneumonia and organ failure. While most people experience mild to moderate symptoms, it can lead to severe illness and even death, particularly among older adults and those with underlying health conditions. To combat the spread of the virus, various preventive measures have been implemented globally, including social distancing, wearing face masks, practicing good hand hygiene, and vaccination efforts.