| ''' |
| 参考: https://github.com/shroominic/codeinterpreter-api |
| |
| 1. 可以存在本地,然后再调出来。 working. |
| 1. 可以在临时文件夹中读取文件。 |
| 1. 可以直接在内存中读出图片。 |
| 1. 中文字体成功。 |
| from matplotlib.font_manager import FontProperties |
| myfont=FontProperties(fname='/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/SimHei.ttf') |
| sns.set_style('whitegrid',{'font.sans-serif':['simhei','Arial']}) |
| |
| |
| ''' |
| |
|
|
| import requests |
| from codeinterpreterapi import CodeInterpreterSession, File |
| import streamlit as st |
| from codeinterpreterapi import CodeInterpreterSession |
| import openai |
| import os |
| import matplotlib.pyplot as plt |
| import xlrd |
| import pandas as pd |
| |
| import tempfile |
| from tempfile import NamedTemporaryFile |
| import pathlib |
| from pathlib import Path |
| from matplotlib.font_manager import FontProperties |
| import seaborn as sns |
|
|
| os.environ["OPENAI_API_KEY"] = "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" |
| openai.api_key = "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" |
| |
| |
| |
|
|
| |
| openai.proxy = { |
| "http": "http://127.0.0.1:7890", |
| "https": "http://127.0.0.1:7890" |
| } |
|
|
| |
| st.title("个人大语言模型商业智能中心") |
| st.subheader("Artificial Intelligence Backend Center for Individuals") |
|
|
| |
|
|
| |
| col1, col2 = st.columns(spec=[1, 2]) |
| radio_1 = col1.radio(label='ChatGPT版本', options=[ |
| 'GPT-3.5', 'GPT-4.0'], horizontal=True, label_visibility='visible') |
| radio_2 = col2.radio(label='模式选择', options=[ |
| '核心模式', '联网模式', '数据模式'], horizontal=True, label_visibility='visible') |
|
|
| |
| def upload_file(uploaded_file): |
| |
| |
| if uploaded_file is not None: |
| filename = uploaded_file.name |
| st.write(filename) |
| try: |
| if '.csv' in filename: |
| csv_file = pd.read_csv(uploaded_file) |
| st.write(csv_file[:3]) |
| else: |
| xls_file = pd.read_excel(uploaded_file) |
| st.write(xls_file[:3]) |
| except Exception as e: |
| st.write(e) |
|
|
| uploaded_file_name = "File_provided" |
| temp_dir = tempfile.TemporaryDirectory() |
| |
| uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name |
| with open(uploaded_file_path, 'wb') as output_temporary_file: |
| |
| |
| output_temporary_file.write(uploaded_file.getvalue()) |
| st.write(uploaded_file_path) |
| output_temporary_file.close() |
|
|
| return uploaded_file_path |
|
|
|
|
| bing_search_api_key = '1fbcda1fb09547efbcdae7cb83a367b5' |
| bing_search_endpoint = 'https://api.bing.microsoft.com/v7.0/search' |
|
|
|
|
| def search(query): |
| |
| |
| mkt = 'zh-CN' |
| params = {'q': query, 'mkt': mkt} |
| headers = {'Ocp-Apim-Subscription-Key': bing_search_api_key} |
|
|
| |
| try: |
| response = requests.get(bing_search_endpoint, |
| headers=headers, params=params) |
| response.raise_for_status() |
| json = response.json() |
| return json["webPages"]["value"] |
| |
| |
| except Exception as e: |
| raise e |
|
|
| |
|
|
| |
| async def text_mode(): |
| |
| if "openai_model" not in st.session_state: |
| st.session_state["openai_model"] = "gpt-3.5-turbo-16k" |
| if radio_1 == 'GPT-3.5': |
| |
| print('radio_1: GPT-3.5 starts!') |
| st.session_state["openai_model"] = "gpt-3.5-turbo-16k" |
| else: |
| print('radio_1: GPT-4.0 starts!') |
| st.session_state["openai_model"] = "gpt-4" |
|
|
| |
| if "messages" not in st.session_state: |
| st.session_state.messages = [] |
|
|
| |
| for message in st.session_state.messages: |
| with st.chat_message(message["role"]): |
| st.markdown(message["content"]) |
|
|
| |
| |
| prompt = st.chat_input("Say something") |
| print('prompt now:', prompt) |
| print('----------'*5) |
| |
| if prompt: |
| st.session_state.messages.append({"role": "user", "content": prompt}) |
| with st.chat_message("user"): |
| st.markdown(prompt) |
|
|
| with st.chat_message("assistant"): |
| message_placeholder = st.empty() |
| full_response = "" |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| if radio_2 == '联网模式': |
| |
| input_message = prompt |
| internet_search_result = search(input_message) |
| search_prompt = [ |
| f"Source:\nTitle: {result['name']}\nURL: {result['url']}\nContent: {result['snippet']}" for result in internet_search_result] |
| prompt = "基于如下的互联网公开信息, 回答问题:\n\n" + \ |
| "\n\n".join(search_prompt[:3]) + "\n\n问题: " + input_message + \ |
| "你需要注意的是回答问题时必须用提问的语言(如英文或者中文)来提示:'答案基于互联网公开信息。'" + "\n\n答案: " |
| |
|
|
| st.session_state.messages.append( |
| {"role": "user", "content": prompt}) |
|
|
| for response in openai.ChatCompletion.create( |
| model=st.session_state["openai_model"], |
| messages=[ |
| {"role": m["role"], "content": m["content"]} |
| for m in st.session_state.messages |
| ], |
| stream=True, |
| ): |
| full_response += response.choices[0].delta.get( |
| "content", "") |
| message_placeholder.markdown(full_response + "▌") |
| message_placeholder.markdown(full_response) |
| st.session_state.messages.append( |
| {"role": "assistant", "content": full_response}) |
|
|
| if radio_2 == '核心模式': |
| print('GPT only starts!!!') |
| print('messages:', st.session_state['messages']) |
| for response in openai.ChatCompletion.create( |
| model=st.session_state["openai_model"], |
| |
| |
| |
| |
| messages=[{'role': 'system', 'content': 'you are ChatGPT'}, {'role': 'user', 'content': prompt}], |
| stream=True, |
| ): |
| full_response += response.choices[0].delta.get( |
| "content", "") |
| message_placeholder.markdown(full_response + "▌") |
| print('session completed!') |
| message_placeholder.markdown(full_response) |
| st.session_state.messages.append( |
| {"role": "assistant", "content": full_response}) |
|
|
|
|
| async def data_mode(uploaded_file_path): |
| print('数据分析模式启动!') |
|
|
| |
| st.write(uploaded_file_path) |
| tmp1 = pd.read_csv(str(uploaded_file_path)) |
| st.write(tmp1[:5]) |
|
|
| |
| if "messages" not in st.session_state: |
| st.session_state.messages = [] |
|
|
| |
| for message in st.session_state.messages: |
| with st.chat_message(message["role"]): |
| st.markdown(message["content"]) |
|
|
| |
| |
| prompt = st.chat_input("Say something") |
| print('prompt now:', prompt) |
| print('----------'*5) |
| |
| if prompt: |
| st.session_state.messages.append({"role": "user", "content": prompt}) |
| with st.chat_message("user"): |
| st.markdown(prompt) |
|
|
| with st.chat_message("assistant"): |
| async with CodeInterpreterSession() as session: |
| |
| |
| environ_settings = """【背景要求】如果我没有告诉你任何定制化的要求,那么请你按照以下的默认要求来回答: |
| ------------------------------------------------------------------------- |
| 1. 你需要用提问的语言来回答(如:中文提问你就用中文来回答,英文提问你就用英文来回答)。 |
| 2. 如果要求你输出图表,那么图的解析度dpi需要设定为300。图尽量使用seaborn库。seaborn库的参数设定:sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark'。 |
| 3. 如果需要显示中文,那么设置如下: |
| 3.1 首先,你需要安装中文字体: |
| myfont=FontProperties(fname='/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/SimHei.ttf') |
| 3.2 然后,你需要设定在matplotlib(plt)和seaborn(sns)中设定: |
| sns.set_style({'font.sans-serif':['Arial','SimHei']}) |
| plt.rcParams['font.sans-serif'] = ['SimHei'] |
| plt.rcParams['font.family']='sans-serif' |
| plt.title(fontsize = 18) |
| ------------------------------------------------------------------------- |
| """ |
|
|
| |
| |
| user_request = environ_settings + "\n\n" + \ |
| "你需要完成以下任务:\n\n" + prompt + "\n\n" \ |
| f"注:文件位置在{uploaded_file_path}" |
| print('user_request: \n', user_request) |
|
|
| |
| files = [File.from_path(str(uploaded_file_path))] |
|
|
| with st.status('thinking...', expanded=True, state='running') as status: |
| |
| response = await session.generate_response( |
| user_request, files=files |
| ) |
|
|
| |
| print("AI: ", response.content) |
| full_response = response.content |
| |
|
|
| |
| for i, file in enumerate(response.files): |
| |
| |
| |
| st.image(file.get_image(), width=None, |
| output_format='PNG') |
|
|
| |
| |
| st.write(full_response) |
| status.update(label='complete', state='complete') |
| |
| |
|
|
| await session.astop() |
| st.session_state.messages.append({"role": "assistant", "content": full_response}) |
|
|
| if __name__ == "__main__": |
| import asyncio |
| if radio_2 != "数据模式": |
| |
| asyncio.run(text_mode()) |
| |
| elif radio_2 == "数据模式": |
| uploaded_file = st.file_uploader( |
| "选择一个文件", type=(["csv", "xlsx", "xls"])) |
| |
| if uploaded_file is not None: |
| uploaded_file_path = upload_file(uploaded_file) |
| asyncio.run(data_mode(uploaded_file_path)) |
| |
|
|
|
|
|
|
|
|