import gradio as gr import os import openai import requests import json openai.api_key = os.environ.get("OPENAI_API_KEY") openai.api_base="https://api.chatanywhere.tech/v1" # OPENAI_API_BASE="https://api.chatanywhere.tech/v1" prompt_templates = {"Default ChatGPT": ""} def get_empty_state(): return {"total_tokens": 0, "messages": []} def download_prompt_templates(): url = "https://raw.githubusercontent.com/JunchuanYu/Sydney/main/prompts.csv" response = requests.get(url) for line in response.text.splitlines()[1:]: act, prompt = line.split('","') prompt_templates[act.replace('"', '')] = prompt.replace('"', '') choices = list(prompt_templates.keys()) return gr.update(value=choices[0], choices=choices) def on_token_change(user_token): openai.api_key = user_token or os.environ.get("OPENAI_API_KEY") def on_prompt_template_change(prompt_template): if not isinstance(prompt_template, str): return return prompt_templates[prompt_template] def submit_message(user_token, prompt, prompt_template, temperature, max_tokens, state): history = state['messages'] if not prompt: return gr.update(value='', visible=state['total_tokens'] < 1_000), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], f"Total tokens used: {state['total_tokens']} / 3000", state prompt_template = prompt_templates[prompt_template] system_prompt = [] if prompt_template: system_prompt = [{ "role": "system", "content": prompt_template }] prompt_msg = { "role": "user", "content": prompt } try: completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history + [prompt_msg], temperature=temperature, max_tokens=max_tokens) history.append(prompt_msg) history.append(completion.choices[0].message.to_dict()) state['total_tokens'] += completion['usage']['total_tokens'] except Exception as e: history.append(prompt_msg) history.append({ "role": "system", "content": f"Error: {e}" }) total_tokens_used_msg = f"Total tokens used: {state['total_tokens']} / 3000" if not user_token else "" chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)] input_visibility = user_token or state['total_tokens'] < 3000 return gr.update(value='', visible=input_visibility), chat_messages, total_tokens_used_msg, state def clear_conversation(): return gr.update(value=None, visible=True), None, "", get_empty_state() css = """ #col-container {max-width: 80%; margin-left: auto; margin-right: auto;} #chatbox {min-height: 400px;} #header {text-align: center;} #prompt_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px;} #total_tokens_str {text-align: right; font-size: 0.8em; color: #666; height: 1em;} #label {font-size: 0.8em; padding: 0.5em; margin: 0;} .message { font-size: 1.2em; } """ with gr.Blocks(css=css) as demo: state = gr.State(get_empty_state()) with gr.Column(elem_id="col-container"): # gr.Markdown(""" # # Sydney-AI #
This app is an intelligent online chat app developed based on the newly released OpenAI API "gpt-3.5-turbo". The app's operating costs are sponsored by "45度科研人". Currently, the tokens is limited to 3000. If you want to remove this restriction, you can input your own OpenAI API key.
#
The default model role of the app is the original assistant of ChatGPT, but you can also choose from the provided roles.
#
Two adjustable parameters are provided to optimize the model: temperature, where a larger value leads to more creative replies, and max tokens, where a larger value allows the model to reply with more content.
# """) gr.Markdown("""