import os import gradio as gr from zhipuai import ZhipuAI SYSTEM_PROMPT = "你是一位智能编程助手,你叫CodeGeeX。你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。" client = ZhipuAI(api_key=os.getenv("CODEGEEX_API_KEY")) def respond(message, history: list[tuple[str, str]]): messages = [{"role": "system", "content": SYSTEM_PROMPT}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) response = "" for message in client.chat.completions.create( messages=messages, # type: ignore model="codegeex-4", stream=True, temperature=0.2, max_tokens=1024, top_p=0.95, ): # type: ignore token = message.choices[0].delta.content response += token yield response with gr.Blocks(fill_height=True) as demo: gr.Markdown( """

""" ) gr.Markdown( """

🏠 Homepage | 📖 Blog | 🛠 VS Code or Jetbrains Extensions | 💻 Github | 🤖 HuggingFace

""" ) gr.Markdown( """

We introduce CodeGeeX4 9B, a large-scale multilingual code generation model with 9 billion parameters, pre-trained on a large code corpus of more than 300 programming languages. CodeGeeX4 9B is open source, please refer to our GitHub for more details. We also offer free VS Code and Jetbrains extensions for full functionality.

""" ) gr.ChatInterface(respond, fill_height=True) if __name__ == "__main__": demo.launch()