mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
ec53d983a1
<!-- Thank you for contributing to LangChain! Replace this entire comment with: - **Description:** a description of the change, - **Issue:** the issue # it fixes (if applicable), - **Dependencies:** any dependencies required for this change, - **Tag maintainer:** for a quicker response, tag the relevant maintainer (see below), - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out! Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` to check this locally. See contribution guidelines for more information on how to write/run tests, lint, etc: https://github.com/langchain-ai/langchain/blob/master/.github/CONTRIBUTING.md If you're adding a new integration, please include: 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/extras` directory. If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17. --> Adding rag-opensearch template. --------- Signed-off-by: kalyanr <kalyan.ben10@live.com> Co-authored-by: Erick Friis <erick@langchain.dev>
61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
import os
|
|
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain.embeddings import OpenAIEmbeddings
|
|
from langchain.prompts import ChatPromptTemplate
|
|
from langchain.pydantic_v1 import BaseModel
|
|
from langchain.schema.output_parser import StrOutputParser
|
|
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
|
|
from langchain.vectorstores.opensearch_vector_search import OpenSearchVectorSearch
|
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
OPENSEARCH_URL = os.getenv("OPENSEARCH_URL", "https://localhost:9200")
|
|
OPENSEARCH_USERNAME = os.getenv("OPENSEARCH_USERNAME", "admin")
|
|
OPENSEARCH_PASSWORD = os.getenv("OPENSEARCH_PASSWORD", "admin")
|
|
OPENSEARCH_INDEX_NAME = os.getenv("OPENSEARCH_INDEX_NAME", "langchain-test")
|
|
|
|
|
|
embedding_function = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
|
|
|
|
vector_store = OpenSearchVectorSearch(
|
|
opensearch_url=OPENSEARCH_URL,
|
|
http_auth=(OPENSEARCH_USERNAME, OPENSEARCH_PASSWORD),
|
|
index_name=OPENSEARCH_INDEX_NAME,
|
|
embedding_function=embedding_function,
|
|
verify_certs=False,
|
|
)
|
|
|
|
|
|
retriever = vector_store.as_retriever()
|
|
|
|
|
|
def format_docs(docs):
|
|
return "\n\n".join([d.page_content for d in docs])
|
|
|
|
|
|
# RAG prompt
|
|
template = """Answer the question based only on the following context:
|
|
{context}
|
|
Question: {question}
|
|
"""
|
|
prompt = ChatPromptTemplate.from_template(template)
|
|
|
|
# RAG
|
|
model = ChatOpenAI(openai_api_key=OPENAI_API_KEY)
|
|
chain = (
|
|
RunnableParallel(
|
|
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
|
)
|
|
| prompt
|
|
| model
|
|
| StrOutputParser()
|
|
)
|
|
|
|
|
|
# Add typing for input
|
|
class Question(BaseModel):
|
|
__root__: str
|
|
|
|
|
|
chain = chain.with_types(input_type=Question)
|