#pip install --upgrade gradio %pip install -q langchain-nvidia-ai-endpoints from langchain_nvidia_ai_endpoints._common import NVEModel from langchain_nvidia_ai_endpoints import ChatNVIDIA from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate ## Simple Chat Pipeline chat_llm = ChatNVIDIA(model="llama2_13b") prompt = ChatPromptTemplate.from_messages([ ("system", "Only respond in rhymes"), ("user", "{input}") ]) rhyme_chain = prompt | chat_llm | StrOutputParser() import gradio as gr ####################################################### ## Non-streaming Interface like that shown above # def rhyme_chat(message, history): # return rhyme_chain.invoke({"input" : message}) # gr.ChatInterface(rhyme_chat).launch() ####################################################### ## Streaming Interface def rhyme_chat_stream(message, history): ## This is a generator function, where each call will yield the next entry buffer = "" for token in rhyme_chain.stream({"input" : message}): buffer += token yield buffer ## Uncomment when you're ready to try this. IF USING COLAB: Share=False is faster gr.ChatInterface(rhyme_chat_stream).queue().launch(share=True, debug=True) ## NOTE: When you're done, please click the Square button (twice to be safe) to stop the session. #This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)#