-
Notifications
You must be signed in to change notification settings - Fork 0
/
rag_example.py
81 lines (62 loc) · 1.89 KB
/
rag_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import boto3
from langchain.embeddings import BedrockEmbeddings
from langchain.vectorstores import FAISS
# Setup bedrock
bedrock_runtime = boto3.client(
service_name="bedrock-runtime",
region_name="us-east-1",
)
sentences = [
# Pets
"Your dog is so cute.",
"How cute your dog is!",
"You have such a cute dog!",
# Cities in the US
"New York City is the place where I work.",
"I work in New York City.",
# Color
"What color do you like the most?",
"What is your favourite color?",
]
def claude_prompt_format(prompt: str) -> str:
# Add headers to start and end of prompt
return "\n\nHuman: " + prompt + "\n\nAssistant:"
# Call Claude model
def call_claude(prompt):
prompt_config = {
"prompt": claude_prompt_format(prompt),
"max_tokens_to_sample": 4096,
"temperature": 0.5,
"top_k": 250,
"top_p": 0.5,
"stop_sequences": [],
}
body = json.dumps(prompt_config)
modelId = "anthropic.claude-v2"
accept = "application/json"
contentType = "application/json"
response = bedrock_runtime.invoke_model(
body=body, modelId=modelId, accept=accept, contentType=contentType
)
response_body = json.loads(response.get("body").read())
results = response_body.get("completion")
return results
def rag_setup(query):
embeddings = BedrockEmbeddings(
client=bedrock_runtime,
model_id="amazon.titan-embed-text-v1",
)
local_vector_store = FAISS.from_texts(sentences, embeddings)
docs = local_vector_store.similarity_search(query)
context = ""
for doc in docs:
context += doc.page_content
prompt = f"""Use the following pieces of context to answer the question at the end.
{context}
Question: {query}
Answer:"""
return call_claude(prompt)
query = "What type of pet do I have?"
print(query)
print(rag_setup(query))