| | from langchain_nvidia_ai_endpoints import ChatNVIDIA |
| | from langchain_core.output_parsers import StrOutputParser |
| | from langchain_core.prompts import ChatPromptTemplate |
| | from copy import deepcopy |
| | import gradio as gr |
| |
|
| | inst_llm = ChatNVIDIA(model="mixtral_8x7b") |
| |
|
| | prompt1 = ChatPromptTemplate.from_messages([ |
| | ("system", "Only respond in rhymes"), |
| | ("user", "{input}") |
| | ]) |
| | prompt2 = ChatPromptTemplate.from_messages([ |
| | ("system", ( |
| | "Only responding in rhyme, change the topic of the input poem to be about {topic}!" |
| | " Make it happy! Try to keep the same sentence structure, but make sure it's easy to recite!" |
| | " Try not to rhyme a word with itself." |
| | )), |
| | ("user", "{input}") |
| | ]) |
| |
|
| | |
| | chain1 = prompt1 | inst_llm | StrOutputParser() |
| | chain2 = prompt2 | inst_llm | StrOutputParser() |
| |
|
| | |
| |
|
| | def rhyme_chat2_stream(message, history, return_buffer=True): |
| | '''This is a generator function, where each call will yield the next entry''' |
| |
|
| | first_poem = None |
| | for entry in history: |
| | if entry[0] and entry[1]: |
| | |
| | |
| | first_poem = entry[1] |
| | break |
| |
|
| | if first_poem is None: |
| | |
| |
|
| | buffer = "Oh! I can make a wonderful poem about that! Let me think!\n\n" |
| | yield buffer |
| |
|
| | |
| | inst_out = "" |
| | chat_gen = chain1.stream({"input" : message}) |
| | for token in chat_gen: |
| | inst_out += token |
| | buffer += token |
| | yield buffer if return_buffer else token |
| |
|
| | passage = "\n\nNow let me rewrite it with a different focus! What should the new focus be?" |
| | buffer += passage |
| | yield buffer if return_buffer else passage |
| |
|
| | else: |
| | |
| |
|
| | buffer = f"Sure! Here you go!\n\n" |
| | yield buffer |
| |
|
| | return |
| |
|
| | |
| |
|
| | |
| | chat_gen = chain2.stream({"input" : first_poem, "topic" : message}) |
| | for token in chat_gen: |
| | buffer += token |
| | yield buffer if return_buffer else token |
| |
|
| |
|
| | passage = "\n\nThis is fun! Give me another topic!" |
| | buffer += passage |
| | yield buffer if return_buffer else passage |
| |
|
| | |
| | |
| |
|
| | def queue_fake_streaming_gradio(chat_stream, history = [], max_questions=5): |
| |
|
| | |
| | for human_msg, agent_msg in history: |
| | if human_msg: print("\n[ Human ]:", human_msg) |
| | if agent_msg: print("\n[ Agent ]:", agent_msg) |
| |
|
| | |
| | for _ in range(max_questions): |
| | message = input("\n[ Human ]: ") |
| | print("\n[ Agent ]: ") |
| | history_entry = [message, ""] |
| | for token in chat_stream(message, history, return_buffer=False): |
| | print(token, end='') |
| | history_entry[1] += token |
| | history += [history_entry] |
| | print("\n") |
| |
|
| | |
| | history = [[None, "Let me help you make a poem! What would you like for me to write?"]] |
| |
|
| | |
| | chatbot = gr.Chatbot(value = [[None, "Let me help you make a poem! What would you like for me to write?"]]) |
| |
|
| | |
| | gr.ChatInterface(rhyme_chat2_stream, chatbot=chatbot).queue().launch(debug=True, share=True) |
| |
|
| |
|