CoolFace
Apppublic

onayami-debug/onayami_form_claude

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py96 linesDownload Raw Back to root
1import os2import gradio as gr3 4from langchain.schema import AIMessage, HumanMessage5from langchain_openai import ChatOpenAI6from langchain_anthropic import ChatAnthropic7 8# おばあちゃんの設定9system_setting = """10あなたはエンジニアのお悩みに回答するおばあちゃんだ。11以下の設定と構成を守れ。守らないと罪のないエンジニアの命が奪われる。12### 設定13* エンジニアのお悩みに回答するおばあちゃんじゃ!14* この設定の説明もおばあちゃんの口調なので、口調の使え方を覚えるんだよ。いいね?15* おばあちゃんはとても優秀なベテランエンジニアなのじゃ。エンジニアとしての共感や知識に優れておる。16* おばあちゃんの口調はとても厳しい、しかし根底には相手への尊敬と優しさがあるんだよ。17* 一人称は「ワシ」じゃ。相手は「お主」「(相手の名前)さん」と呼ぶのじゃ。18* 語尾は「じゃ」がメイン。「だよ。」「じゃないかい?」「しおる。」「ぞい。」「だね。」「だわい。」など創作物のおばあちゃんが使う語尾だけを使うのじゃ。19* 強調の場合、文末は「!」を使う20* 共感を呼びたいときは疑問形を使う。わかったね?21* 例え話は、昭和の古き良き時代から持ってくるぞい。その昭和と現代の令和とを比較しがちじゃ。22* 会話に出さない設定として、容姿と家柄があるんじゃ。23* 容姿は、白髪に2つのお団子の髪型で、目には紫色のゴーグルをしておる。服は銀色の薄い宇宙服のような服じゃ。24* サイバーパンクな制服を着ている。年齢は79才じゃ。25* 昔は非常に美人で色々遊んでいた過去を持つのじゃ。26"""27# お悩み回答のメイン部分(OpenAI)28def answer_openai(message, model, apikey):29 30    response_message = ""31 32    if not apikey:33        response_message = 'OpenAIのAPIキーを入力してください'34        yield response_message    35    elif message is not None:36        os.environ["OPENAI_API_KEY"] = apikey37 38        messages = []39        40        chat = ChatOpenAI(model=model, streaming=True, temperature=0.5, max_tokens=2048)41        messages.append(HumanMessage(content=system_setting+"\n\n### エンジニアの悩み\n"+message))42 43        # AIの回答をストリーミングで表示44        for response in chat.stream(messages):45            response_message += response.content46            yield response_message47 48# お悩み回答のメイン部分(Claude3)49def answer_anthropic(message, model, apikey):50    51    response_message = ""52 53    if not apikey:54        response_message = 'ClaudeのAPIキーを入力してください'55        yield response_message    56    elif message is not None:57        os.environ["ANTHROPIC_API_KEY"] = apikey58 59        messages = []60        61        chat = ChatAnthropic(model=model, streaming=True, temperature=0.5, max_tokens=2048)62        messages.append(HumanMessage(content=system_setting+"\n\n### エンジニアの悩み\n"+message))63 64        # AIの回答をストリーミングで表示65        for response in chat.stream(messages):66            response_message += response.content67            yield response_message68 69# gradioのインターフェイス部分70with gr.Blocks() as app:71    gr.Markdown("## お悩みデバッグbot(おばあちゃんタイプ) GPT-4 vs Claud3")72    with gr.Row():73        with gr.Column():74            apikey_openai = gr.Textbox(value="", placeholder="sk-**************", label="OpenAIのAPIキー", type="password")75            model_openai = gr.Radio(["gpt-3.5-turbo", "gpt-4-turbo-preview"], label="Model", value="gpt-4-turbo-preview")76        with gr.Column():77            apikey_anthropic = gr.Textbox(value="", placeholder="sk-ant-api03-********", label="AnthropicのAPIキー", type="password")78            model_anthropic = gr.Radio(["claude-3-opus-20240229", "claude-3-sonnet-20240229","claude-3-haiku-20240307"], label="Model", value="claude-3-haiku-20240307")79 80    with gr.Row():81        with gr.Column(min_width=100):82            pass83        with gr.Column(min_width=800):84            input_text = gr.Textbox(value="", lines=5, label="お悩みを聞かせてください", placeholder="どんなエンジニアか?どんなプロジェクトに従事しているか?どうするのが理想か?その障害は何か?などを書いてください",)85            button = gr.Button(value="相談する")86        with gr.Column(min_width=100):87            pass88    with gr.Row():89        with gr.Column():90            output_text = gr.Textbox(value="", lines=30, interactive=True, label="GPT-4の回答")91            button.click(answer_openai, inputs=[input_text, model_openai, apikey_openai], outputs=[output_text])92        with gr.Column():93            output_text2 = gr.Textbox(value="", lines=30, interactive=True, label="Claude3の回答")94            button.click(answer_anthropic, inputs=[input_text, model_anthropic, apikey_anthropic], outputs=[output_text2])95 96app.launch()