-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
taipy==3.0.0 | ||
openai==1.3.7 |