#Import dependancies import os from typing import Optional, Tuple import gradio as gr from langchain import PromptTemplate, HuggingFaceHub, LLMChain from langchain.memory import ConversationBufferWindowMemory from langchain.chains import ConversationChain from langchain.llms import OpenAI import random import time template = """Assistant is a large language model trained by Inwi. Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist. {history} Human: {human_input} Assistant:""" prompt = PromptTemplate(template=template, input_variables=["history","human_input"]) llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1e-10}) llm_chain = LLMChain( llm=llm, prompt=prompt, verbose=True, memory=ConversationBufferWindowMemory(k=2) ) def load_chain(): """Logic for loading the chain you want to use should go here.""" llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1e-10}) llm_chain = LLMChain( llm=llm, prompt=prompt, verbose=True, memory=ConversationBufferWindowMemory(k=2) ) return llm_chain def chat( inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain] ): """Execute the chat functionality.""" history = history or [] output = llm_chain.predict(human_input=inp) history.append((inp, output)) return history, history block = gr.Blocks(css=".gradio-container {background-color: lightgray}") with block: with gr.Row(): gr.Markdown("

LangChain Demo

") chatbot = gr.Chatbot() with gr.Row(): message = gr.Textbox( label="What's your question?", placeholder="What's the answer to life, the universe, and everything?", lines=1, ) submit = gr.Button(value="Send", variant="secondary").style(full_width=False) gr.Examples( examples=[ "Hi! How's it going?", "What should I do tonight?", "Whats 2 + 2?", ], inputs=message, ) gr.HTML("Demo application of a LangChain chain.") gr.HTML( "
Powered by LangChain 🦜️🔗
" ) state = gr.State() agent_state = gr.State() submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state]) message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state]) block.launch(debug=True)