|
from openai import OpenAI |
|
import streamlit as st |
|
import os |
|
from datetime import datetime |
|
|
|
|
|
client = OpenAI( |
|
base_url="https://integrate.api.nvidia.com/v1", |
|
api_key=os.environ["NVIDIA_API_KEY"] |
|
) |
|
|
|
st.title("ChatGPT-like clone with Nvidia Nemotron 70B Model") |
|
|
|
|
|
with st.sidebar: |
|
|
|
st.markdown("### Instructions 🤖\nThis is a basic chatbot. Ask anything, and the AI will try to help you! The app is supported by Yiqiao Yin.") |
|
|
|
|
|
st.markdown("#### Select the desired length of the AI response:") |
|
response_length = st.radio( |
|
"How detailed do you want the response to be?", |
|
('Efficient', 'Medium', 'Academic') |
|
) |
|
|
|
|
|
if response_length == 'Efficient': |
|
max_tokens = 100 |
|
elif response_length == 'Medium': |
|
max_tokens = 600 |
|
else: |
|
max_tokens = 1024 |
|
|
|
|
|
if st.button("Clear Session"): |
|
st.session_state.clear() |
|
st.write(f"Copyright © 2010-{datetime.now().year} Present Yiqiao Yin") |
|
|
|
|
|
if "nvidia_model" not in st.session_state: |
|
st.session_state["nvidia_model"] = "nvidia/llama-3.1-nemotron-70b-instruct" |
|
|
|
if "messages" not in st.session_state: |
|
|
|
st.session_state.messages = [{"role": "system", "content": "You are a helpful assistant."}] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
if prompt := st.chat_input("What is up?"): |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
with st.chat_message("user"): |
|
st.markdown(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
with st.spinner("The assistant is thinking... Please wait."): |
|
|
|
stream = client.chat.completions.create( |
|
model=st.session_state["nvidia_model"], |
|
messages=st.session_state.messages, |
|
temperature=0.5, |
|
top_p=0.7, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
) |
|
response_chunks = [] |
|
for chunk in stream: |
|
if chunk.choices[0].delta.content is not None: |
|
response_chunks.append(chunk.choices[0].delta.content) |
|
response = "".join(response_chunks) |
|
st.markdown(response) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|