import gradio as gr # from langchain.llms import OpenAI # from langchain.llms.fake import FakeListLLM # from langchain import PromptTemplate, FewShotPromptTemplate # from langchain.utilities import TextRequestsWrapper # import json # def generate_prompt(embedding_url, extra_prompt, count): # output = TextRequestsWrapper().get(embedding_url) # result = json.loads(output) # example_formatter_template = "标题: {sentence}\n热度: {hot_level}" # example_prompt = PromptTemplate( # input_variables=["sentence", "hot_level"], # template=example_formatter_template, # ) # few_shot_prompt = FewShotPromptTemplate( # examples=result["data"], # example_prompt=example_prompt, # prefix="你是一个短视频方专家,你的职责是为短视频的制作进行选题。\n以下是某视频站点的最新热词\n\n", # # The suffix is some text that goes after the examples in the prompt. # # Usually, this is where the user input will go # suffix="{extra_prompt}\n\n请根据以上内容草拟{count}个吸引人的短视频的标题:", # # The input variables are the variables that the overall prompt expects. # input_variables=["extra_prompt", "count"], # # The example_separator is the string we will use to join the prefix, examples, and suffix together with. # example_separator="\n\n", # ) # # We can now generate a prompt using the `format` method. # # print(few_shot_prompt.format(count="5")) # return few_shot_prompt.format(extra_prompt=extra_prompt, count=count) # def generate_topics( # openai_api_key: str, # embedding_url: str, # extra_prompt: str, # count: int, # repeat: int # ): # if openai_api_key.strip() == "": # return "请输入OPENAI API KEY" # llm = OpenAI(temperature=.7, openai_api_key=openai_api_key) # prompt = generate_prompt(embedding_url, extra_prompt, count) # print(prompt) # llm_result = llm.generate([prompt]*repeat) # answer = "" if repeat == 1 else f"以下是合并的{str(repeat)}次选题结果:\n\n" # for i in range(len(llm_result.generations)): # answer += f"{'下一个:' if i > 0 else ''}" + llm_result.generations[i][0].text + "\n\n" # print(answer) # token_usage = llm_result.llm_output["token_usage"]["total_tokens"] if llm_result.llm_output else 0 # answer += f"##总共消耗Token数:{str(token_usage)}" # return answer with gr.Blocks() as demo: gr.HTML("""