forked from paolob67/chat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-app.py
48 lines (41 loc) · 1.97 KB
/
chat-app.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
import streamlit as st
from ibm_watson_machine_learning.foundation_models import Model
import json
import os
st.title('Watsonx Chatbot 🤖')
st.caption("🚀 A chatbot powered by watsonx.ai - rel 13")
with st.sidebar:
watsonx_api_key = st.text_input("Watsonx API Key", key="watsonx_api_key", value=os.environ.get("API_KEY"), type="password")
watsonx_url = st.text_input("Watsonx URL", key="watsonx_url", value="https://us-south.ml.cloud.ibm.com", type="default")
#TODO: change this to a select box with more than one model
watsonx_model = st.text_input("Model", key="watsonx_model", value="meta-llama/llama-2-70b-chat", type="default")
watsonx_model_params = st.text_input("Params", key="watsonx_model_params", value='{"decoding_method":"sample", "max_new_tokens":200, "temperature":0.5}', type="default" )
if not watsonx_api_key:
st.info("Please add your watsonx API key to continue.")
else :
st.info("setting up to use: " + watsonx_model)
my_credentials = {
"url" : watsonx_url,
"apikey" : watsonx_api_key
}
params = json.loads(watsonx_model_params)
project_id = os.environ.get("PRJ_ID")
space_id = None
verify = False
model = Model( watsonx_model, my_credentials, params, project_id, space_id, verify )
if model :
st.info("done")
if 'messages' not in st.session_state:
st.session_state.messages = [{"role": "assistant", "content": "How can I help you?"}]
for message in st.session_state.messages:
st.chat_message(message['role']).markdown(message['content'])
prompt = st.chat_input('Pass Your Prompt here')
if prompt:
st.chat_message('user').markdown(prompt)
st.session_state.messages.append({'role':'user', 'content':prompt})
if model :
response = model.generate_text(prompt)
else :
response = "You said: " + prompt
st.chat_message('assistant').markdown(response)
st.session_state.messages.append({'role':'assistant', 'content':response})