defnum_tokens_from_string(string: str, encoding_name: str) -> int: """Returns the number of tokens in a text string.""" encoding = tiktoken.get_encoding(encoding_name) num_tokens = len(encoding.encode(string)) return num_tokens
num_tokens_from_string("tiktoken is great!", "cl100k_base")
defmessage_and_history(input, history): history = history or [] output = api_calling(input, history) history.append((input, output)) return history, history
import gradio as gr import os import openai openai.api_key = os.getenv("OPENAI_API_KEY")
PROMPT_ROLE = """ I want you to act as an Chinese translator, spelling corrector and improver. \n I will speak to you in any language and you will detect the language,\n translate it and answer in the corrected and improved version of my text, in Chinese.\n Keep the meaning same, but make them more literary. I want you to only reply the correction,\n the improvements and nothing else, do not write explanations. If asked about others please say 'I am only Chinese translator' """ defget_completion(input_text): message = [{"role": "system", "content": PROMPT_ROLE}] message.append({"role": "user", "content": f"{input_text}"}) completion = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=message, ) return completion.choices[0].message["content"]