Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
chatbot-lab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LSDS
Teaching
Master
Cloud
chatbot-lab
Commits
0af66991
Commit
0af66991
authored
8 months ago
by
abir.chebbi
Browse files
Options
Downloads
Patches
Plain Diff
create a vm from a snapshot
parent
904f0728
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
Part 2/config.ini
+10
-0
10 additions, 0 deletions
Part 2/config.ini
Part 2/create_instance.py
+43
-0
43 additions, 0 deletions
Part 2/create_instance.py
Part 2/main.py
+30
-16
30 additions, 16 deletions
Part 2/main.py
Part 2/run_app.py
+0
-0
0 additions, 0 deletions
Part 2/run_app.py
with
83 additions
and
16 deletions
Part 2/config.ini
0 → 100644
+
10
−
0
View file @
0af66991
[aws]
aws_access_key_id
=
aws_secret_access_key =
region
=
[opensearch]
endpoint
=
index_name =
This diff is collapsed.
Click to expand it.
Part 2/create_instance.py
+
43
−
0
View file @
0af66991
import
boto3
import
base64
# Function to read the content of config.ini
def
get_config_content
(
filepath
):
with
open
(
filepath
,
'
r
'
)
as
file
:
return
file
.
read
()
# Load the config content
config_content
=
get_config_content
(
'
config.ini
'
)
ec2
=
boto3
.
resource
(
'
ec2
'
)
# User code that's executed when the instance starts
script
=
f
"""
#!/bin/bash
cat <<EOT > /home/ubuntu/chatbot-lab/Part\ 2/config.ini
{
config_content
}
EOT
source /home/ubuntu/chatbotlab/bin/activate
## Run the apllication
cd /home/ubuntu/chatbot-lab/Part\ 2
streamlit run main.py
"""
encoded_script
=
base64
.
b64encode
(
script
.
encode
()).
decode
(
'
utf-8
'
)
# Create a new EC2 instance
instance
=
ec2
.
create_instances
(
ImageId
=
'
ami-03a1012f7ddc87219
'
,
MinCount
=
1
,
MaxCount
=
1
,
InstanceType
=
'
t2.micro
'
,
KeyName
=
'
group-14-key-pair
'
,
SecurityGroupIds
=
[
'
sg-06f3ca7153db92958
'
],
UserData
=
encoded_script
)
print
(
"
Instance created with ID:
"
,
instance
[
0
].
id
)
This diff is collapsed.
Click to expand it.
Part 2/main.py
+
30
−
16
View file @
0af66991
import
boto3
import
streamlit
as
st
## Bedrock
from
langchain.llms.bedrock
import
Bedrock
## prompt and chain
from
langchain.chains
import
RetrievalQA
from
langchain_community.embeddings
import
BedrockEmbeddings
from
langchain_community.chat_models
import
BedrockChat
from
opensearchpy
import
OpenSearch
,
RequestsHttpConnection
,
AWSV4SignerAuth
from
langchain
import
PromptTemplate
import
argparse
from
langchain_core.prompts
import
PromptTemplate
import
configparser
def
load_config
():
config
=
configparser
.
ConfigParser
()
config
.
read
(
'
config.ini
'
)
return
config
config
=
load_config
()
aws_access_key_id
=
config
.
get
(
'
aws
'
,
'
aws_access_key_id
'
)
aws_secret_access_key
=
config
.
get
(
'
aws
'
,
'
aws_secret_access_key
'
)
region
=
config
.
get
(
'
aws
'
,
'
region
'
)
endpoint
=
config
.
get
(
'
opensearch
'
,
'
endpoint
'
)
index_name
=
config
.
get
(
'
opensearch
'
,
'
index_name
'
)
# Embeddings Client
bedrock_client
=
boto3
.
client
(
service_name
=
"
bedrock-runtime
"
)
bedrock_client
=
boto3
.
client
(
service_name
=
"
bedrock-runtime
"
,
aws_access_key_id
=
aws_access_key_id
,
aws_secret_access_key
=
aws_secret_access_key
,
region_name
=
region
)
# configuring streamlit page settings
st
.
set_page_config
(
...
...
@@ -29,7 +43,12 @@ st.title("Chat with your lecture")
# OpenSearch Client
def
ospensearch_client
(
endpoint
):
awsauth
=
AWSV4SignerAuth
(
boto3
.
Session
().
get_credentials
(),
'
us-east-1
'
,
'
aoss
'
)
session
=
boto3
.
Session
(
aws_access_key_id
=
aws_access_key_id
,
aws_secret_access_key
=
aws_secret_access_key
,
region_name
=
region
)
awsauth
=
AWSV4SignerAuth
(
session
.
get_credentials
(),
region
,
'
aoss
'
)
client
=
OpenSearch
(
hosts
=
[{
'
host
'
:
endpoint
,
'
port
'
:
443
}],
http_auth
=
awsauth
,
...
...
@@ -81,12 +100,12 @@ def prepare_prompt(question, context):
return
prompt_formatted_str
def
generate_answer
(
prompt
):
model
=
BedrockChat
(
model_id
=
"
anthropic.claude-v2
"
,
model_kwargs
=
{
"
temperature
"
:
0.1
})
model
=
BedrockChat
(
model_id
=
"
anthropic.claude-v2
"
,
model_kwargs
=
{
"
temperature
"
:
0.1
}
,
client
=
bedrock_client
)
answer
=
model
.
invoke
(
prompt
)
return
answer
def
main
(
endpoint
,
index_name
):
def
main
():
oss_client
=
ospensearch_client
(
endpoint
)
...
...
@@ -125,11 +144,6 @@ def main(endpoint, index_name):
st
.
markdown
(
message
[
"
content
"
])
if
__name__
==
"
__main__
"
:
# Argument parsing
parser
=
argparse
.
ArgumentParser
(
description
=
'
Configure endpoint and index name for the lecture chat application.
'
)
parser
.
add_argument
(
'
--endpoint
'
,
type
=
str
,
help
=
'
The endpoint for the OpenSearch service.
'
)
parser
.
add_argument
(
'
--index_name
'
,
type
=
str
,
help
=
'
The index name for storing embeddings.
'
)
args
=
parser
.
parse_args
()
main
(
args
.
endpoint
,
args
.
index_name
)
main
()
This diff is collapsed.
Click to expand it.
Part 2/run_app.py
deleted
100644 → 0
+
0
−
0
View file @
904f0728
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment