Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples for the In-Memory VectorStore Driver #110

Merged
merged 4 commits into from
Jul 26, 2024

Conversation

joehu21
Copy link

@joehu21 joehu21 commented Jul 10, 2024

Updated samples for the In-Memory VectorStore Driver, and added the missing PDF test file. Manually tested with MemoryDB 7.1.1 cluster.

samples/inmemory/retriever.ipynb Outdated Show resolved Hide resolved
Comment on lines 42 to 43
"from langchain_aws.embeddings import BedrockEmbeddings\n",
"from langchain_aws.llms.bedrock import Bedrock\n",
"from langchain_aws.llms.bedrock import BedrockLLM\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify the import.

from langchain_aws.llms import BedrockLLM

Also, why are we not using the ChatBedrock class?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried changing it to ChatBedrock, but the RetrievalQA code below failed. I think the code below also needs to change. Will investigate when I get some free time. It is a learning opportunity for me. We can leave it as is for now.

qa_prompt = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vds.as_retriever(),
    return_source_documents=True,
    chain_type_kwargs={"prompt": PROMPT},
    #verbose="true"
)
query = "How do i create a MemoryDB cluster"
result = qa_prompt({"query": query})

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RetrievalQA chain has been deprecated. Use this instead.

from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_aws import ChatBedrock


retriever = ...  # Your retriever
llm = ChatBedrock(...)

system_prompt = (
    "Use the given context to answer the question. "
    "If you don't know the answer, say you don't know. "
    "Use three sentence maximum and keep the answer concise. "
    "Context: {context}"
)
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system_prompt),
        ("human", "{input}"),
    ]
)
question_answer_chain = create_stuff_documents_chain(llm, prompt)
chain = create_retrieval_chain(retriever, question_answer_chain)

chain.invoke({"input": query})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still getting the same error:

ValueError: Error raised by bedrock service: An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: #: subject must not be valid against schema {"required":["messages"]}#: extraneous key [max_tokens_to_sample] is not permitted, please reformat your input and try again.

code:

system_prompt = (
    "Use the given context to answer the question. "
    "If you don't know the answer, say you don't know. "
    "Use three sentence maximum and keep the answer concise. "
    "Context: {context}"
)
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system_prompt),
        ("human", "{input}"),
    ]
)
question_answer_chain = create_stuff_documents_chain(llm, prompt)
chain = create_retrieval_chain(retriever, question_answer_chain)
query = "How do i create a MemoryDB cluster?"
chain.invoke({"input": query})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the offline help. Pushed the 3rd commit to change to ChatBedrock and ChatPromptTemplate.

"from langchain_aws.embeddings import BedrockEmbeddings\n",
"from langchain_aws.llms.bedrock import Bedrock\n",
"from langchain_aws.llms.bedrock import BedrockLLM\n",
"load_dotenv()"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above. Even if you want to use the dotenv, the correct syntax would be to use the magics in a new cell. You don't need the import above for this.

%load_ext dotenv
%dotenv

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know.

" model_id=\"anthropic.claude-v2\", #use the Anthropic Claude model\n",
"# use the Anthropic Claude model\n",
"llm = BedrockLLM(\n",
" model_id=\"anthropic.claude-v2\",\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users are most likely to use claude-3 models.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to change it to claude-3, but it fails:

ValueError: Error raised by bedrock service: An error occurred (ValidationException) when calling the InvokeModel operation: The provided model identifier is invalid.

Here's my test:

from langchain_aws import BedrockLLM

# initialize the Bedrock LLM
llm = BedrockLLM(
    model_id="anthropic.claude-v3"
)

prompt = "What is the largest city in Vermont?"

# return a response to the prompt
response = llm.invoke(prompt)
print(response)

Any idea why the above fails?

BTW, langchain-aws README still uses claude-2.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That model id seems wrong. You can use one of these.

anthropic.claude-3-haiku-20240307-v1:0
anthropic.claude-3-opus-20240229-v1:0
anthropic.claude-3-sonnet-20240229-v1:0

Also, can you use ChatBedrock.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

samples/inmemory/retriever.ipynb Show resolved Hide resolved
samples/inmemory/retriever.ipynb Outdated Show resolved Hide resolved
samples/inmemory/retriever.ipynb Outdated Show resolved Hide resolved
@3coins 3coins merged commit 767fa5a into langchain-ai:main Jul 26, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants