|
| 1 | +import os |
| 2 | + |
| 3 | +import openai |
| 4 | +import streamlit as st |
| 5 | + |
| 6 | +from .utils import CodeKernel, extract_code, execute, postprocess_text |
| 7 | + |
| 8 | + |
| 9 | +@st.cache_resource |
| 10 | +def get_kernel(): |
| 11 | + return CodeKernel() |
| 12 | + |
| 13 | + |
| 14 | +SYSTEM_MESSAGE = [ |
| 15 | + { |
| 16 | + "role": "system", |
| 17 | + "content": "你是一位智能AI助手,你叫ChatGLM,你连接着一台电脑,但请注意不能联网。在使用Python解决任务时,你可以运行代码并得到结果,如果运行结果有错误,你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件,文件默认存储路径是/mnt/data/。" |
| 18 | + } |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | +def chat_once(message_placeholder): |
| 23 | + params = dict( |
| 24 | + model="chatglm3", |
| 25 | + messages=SYSTEM_MESSAGE + st.session_state.messages, |
| 26 | + stream=True, |
| 27 | + max_tokens=st.session_state.get("max_tokens", 512), |
| 28 | + temperature=st.session_state.get("temperature", 0.9), |
| 29 | + ) |
| 30 | + response = openai.ChatCompletion.create(**params) |
| 31 | + |
| 32 | + display = "" |
| 33 | + for _ in range(5): |
| 34 | + full_response = "" |
| 35 | + for chunk in response: |
| 36 | + content = chunk.choices[0].delta.get("content", "") |
| 37 | + full_response += content |
| 38 | + display += content |
| 39 | + message_placeholder.markdown(postprocess_text(display) + "▌") |
| 40 | + |
| 41 | + if chunk.choices[0].finish_reason == "stop": |
| 42 | + message_placeholder.markdown(postprocess_text(display) + "▌") |
| 43 | + st.session_state.messages.append( |
| 44 | + { |
| 45 | + "role": "assistant", |
| 46 | + "content": full_response |
| 47 | + } |
| 48 | + ) |
| 49 | + return |
| 50 | + |
| 51 | + elif chunk.choices[0].finish_reason == "function_call": |
| 52 | + try: |
| 53 | + code = extract_code(full_response) |
| 54 | + except: |
| 55 | + continue |
| 56 | + |
| 57 | + with message_placeholder: |
| 58 | + with st.spinner("Executing code..."): |
| 59 | + try: |
| 60 | + res_type, res = execute(code, get_kernel()) |
| 61 | + except Exception as e: |
| 62 | + st.error(f"Error when executing code: {e}") |
| 63 | + return |
| 64 | + |
| 65 | + if res_type == "text": |
| 66 | + res = postprocess_text(res) |
| 67 | + display += "\n" + res |
| 68 | + message_placeholder.markdown(postprocess_text(display) + "▌") |
| 69 | + elif res_type == "image": |
| 70 | + st.image(res) |
| 71 | + |
| 72 | + st.session_state.messages.append( |
| 73 | + { |
| 74 | + "role": "assistant", |
| 75 | + "content": full_response, |
| 76 | + "function_call": {"name": "interpreter", "arguments": ""}, |
| 77 | + } |
| 78 | + ) |
| 79 | + st.session_state.messages.append( |
| 80 | + { |
| 81 | + "role": "function", |
| 82 | + "content": "[Image]" if res_type == "image" else res, # 调用函数返回结果 |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + break |
| 87 | + |
| 88 | + params["messages"] = st.session_state.messages |
| 89 | + response = openai.ChatCompletion.create(**params) |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + st.title("💬 Code Interpreter") |
| 94 | + |
| 95 | + openai.api_base = os.getenv("INTERPRETER_CHAT_API_BASE", "http://192.168.20.59:7891/v1") |
| 96 | + openai.api_key = os.getenv("API_KEY", "xxx") |
| 97 | + |
| 98 | + if "messages" not in st.session_state: |
| 99 | + st.session_state.messages = [] |
| 100 | + |
| 101 | + for message in st.session_state.messages: |
| 102 | + role = message["role"] |
| 103 | + if role in ["user", "function"]: |
| 104 | + with st.chat_message("user"): |
| 105 | + st.markdown(message["content"]) |
| 106 | + else: |
| 107 | + with st.chat_message("assistant"): |
| 108 | + st.markdown(postprocess_text(message["content"])) |
| 109 | + |
| 110 | + if prompt := st.chat_input("What is up?"): |
| 111 | + st.session_state.messages.append({"role": "user", "content": prompt}) |
| 112 | + with st.chat_message("user"): |
| 113 | + st.markdown(prompt) |
| 114 | + |
| 115 | + with st.chat_message("assistant"): |
| 116 | + message_placeholder = st.empty() |
| 117 | + chat_once(message_placeholder) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments