Skip to content

Commit

Permalink
added chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
sharronq committed Feb 11, 2024
1 parent 667485c commit 75e5199
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
60 changes: 60 additions & 0 deletions chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from taipy.gui import Gui, State, notify
import openai

context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today? "
conversation = {
"Conversation": ["Who are you?", "Hi! I am GPT-3. How can I help you today?"]
}
current_user_message = ""

client = openai.Client(api_key="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")

def request(state: State, prompt: str) -> str:
"""
Send a prompt to the GPT-3 API and return the response.
Args:
- state: The current state.
- prompt: The prompt to send to the API.
Returns:
The response from the API.
"""
response = state.client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"{prompt}",
}
],
model="gpt-3.5-turbo",
)
return response.choices[0].message.content

def send_message(state: State) -> None:
"""
Send the user's message to the API and update the conversation.
Args:
- state: The current state.
"""
# Add the user's message to the context
state.context += f"Human: \n {state.current_user_message}\n\n AI:"
# Send the user's message to the API and get the response
answer = request(state, state.context).replace("\n", "")
# Add the response to the context for future messages
state.context += answer
# Update the conversation
conv = state.conversation._dict.copy()
conv["Conversation"] += [state.current_user_message, answer]
state.conversation = conv
# Clear the input field
state.current_user_message = ""

page = """
<|{conversation}|table|show_all|width=100%|>
<|{current_user_message}|input|label=Write your message here...|on_action=send_message|class_name=fullwidth|>
"""


Gui(page).run(title="Taipy Chat", use_reloader=True, port=5001)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
taipy==3.0.0
openai==1.3.7

0 comments on commit 75e5199

Please sign in to comment.