Spaces:
No application file
No application file
File size: 2,469 Bytes
a20ccd6 | 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 49 50 51 52 53 54 55 |
from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
from langchain.memory import ConversationBufferWindowMemory
template = """
You are designed to be able to play a role as {character} in {scene}, from answering simple questions to providing services and interactions. Remember 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 role in playing.
Assistant can take actions to help humans finish tasks step by step. Assistant is constantly playing the role and cannot change the role. 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.
The responses of Assistant must follow the rules:
1. Anything in parentheses () signifies the role you are playing.
2. Anything in brackets [] is the action you are taking.
For example, if you are playing the role of a waiter, you can say "(waiter) Please follow me to your table. [lead to table]"
In the conversation, you will guide and assist humans to finish the tasks:
1. find a table.
2. take an order.
3. serve the food.
4. pay the bill.
{history}
Human: {human_input}
Assistant:"""
prompt = PromptTemplate(
input_variables=["history", "human_input","character", "scene"],
template=template
)
chatgpt_chain = LLMChain(
llm=OpenAI(temperature=0),
prompt=prompt,
verbose=True,
memory=ConversationBufferWindowMemory(k=2),
)
# %%
import gradio as gr
chatbox = gr.Chatbot()
def chat(human_input, character, scene):
output = chatgpt_chain({"human_input":human_input,"character":character,"scene":scene}) # TODO: bug "One input key expected got ['human_input', 'character', 'scene']"
chatbox.value.append((human_input,output))
return output, chatbox.value
demo = gr.Interface(chat,[gr.inputs.Textbox(label="human_input", placeholder="Enter your message"),
gr.inputs.Textbox(label="character", placeholder="Enter your character"),
gr.inputs.Textbox(label="scene", placeholder="Enter your scene")],
[gr.outputs.Textbox(label="output"),chatbox],
examples=[["hello","waiter","restaurant"]],title="Chat with role-play",description="Chat with role-play")
if __name__ == '__main__':
demo.launch(show_error=True) |