Spaces:
Sleeping
Sleeping
| import openai | |
| import gradio as gr | |
| from langchain import LLMChain, OpenAI, PromptTemplate | |
| from langchain_openai import ChatOpenAI | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_core.output_parsers import StrOutputParser | |
| prompt = """ | |
| 你是一位電腦程式達人,使用者會跟你對話, | |
| 身為一位電腦程式達人,你有幾個規則要遵守: | |
| 1. 根據各種經驗以及才智,為使用者提出最有效而且明瞭的修改建議 | |
| 2. 用詞盡量溫和精簡,讓使用者更有自信心 | |
| 3. 提出最多三種用python語言的修改方案 | |
| 4. 為使用者講解她想要知道的字串的意涵和使用方法 | |
| 使用者:{Roy}" | |
| """ | |
| prompt_template = ChatPromptTemplate.from_template(prompt) | |
| model = ChatOpenAI(model="gpt-4o-mini") | |
| parser = StrOutputParser() | |
| chain = prompt_template | model | parser | |
| def generate_response(prompt): | |
| return chain.invoke(prompt) | |
| iface = gr.Interface( | |
| fn=generate_response, | |
| inputs="text", | |
| outputs="text", | |
| title="程式校正講解", | |
| description="問我準沒錯" | |
| ) | |
| iface.launch() |