Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

an example may be helpful for you #55

Closed
Komorebi-yaodong opened this issue Apr 12, 2024 · 0 comments
Closed

an example may be helpful for you #55

Komorebi-yaodong opened this issue Apr 12, 2024 · 0 comments

Comments

@Komorebi-yaodong
Copy link

I wrote an example in Python with streamlit, may be help for you

import streamlit as st
import requests
import base64
import json
import os

api_key = ""
base_url = "http://localhost:8000/v1/chat/completions"
model = "assistant"
plain_text = ["txt","c","cpp","py","js","java"]


show_chat = st.container()

if "message_history" not in st.session_state:
    st.session_state.message_history = []
    st.session_state.message_history_init = []
if "use_search" not in st.session_state:
    st.session_state.use_search = True
if "file_upload" not in st.session_state:
    st.session_state.file_upload = None


def process_files(files:list):
    # Processing images exclusively
    files = [(f.name,f.name.split(".")[-1],f.getvalue()) for f in files]
    
    # we need base64 encoded files:pdf、doc、xlsx、ppt、txt
    files_base64 = []
    f_file = []
    for fb in files:
        isimage = False
        fb_base64 = base64.b64encode(fb[2]).decode('utf-8')
        # fb_base64 = fb[2]
        if fb[1] == "png":
            url = f"data:image/png;base64,{fb_base64}"
            isimage = True
        elif fb[1] == "jpg" or fb[1] == "jpeg":
            url = f"data:image/jpeg;base64,{fb_base64}"
            isimage = True
        elif fb[1] == "gif":
            url = f"data:image/gif;base64,{fb_base64}"
            isimage = True
        elif fb[1] == "webp":
            url = f"data:image/webp;base64,{fb_base64}"
            isimage = True
        elif fb[1] == "bmp ":
            url = f"data:image/bmp;base64,{fb_base64}"
            isimage = True
        elif fb[1] == "pdf":
            url = f"data:application/pdf;base64,{fb_base64}"
        elif fb[1] == "doc":
            url = f"data:application/msword;base64,{fb_base64}"
        elif fb[1] == "docx":
            url = f"data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{fb_base64}"
        elif fb[1] == "xls":
            url = f"data:application/vnd.ms-excel;base64,{fb_base64}"
        elif fb[1] == "xlsx":
            url = f"data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{fb_base64}"
        elif fb[1] == "ppt":
            url = f"data:application/vnd.ms-powerpoint;base64,{fb_base64}"
        elif fb[1] == "pptx":
            url = f"data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,{fb_base64}"
        elif fb[1] == "html":
            url = f"data:text/html;base64,{fb_base64}"
        elif fb[1] == "js":
            url = f"data:text/javascript;base64,{fb_base64}"
        elif fb[1] == "css":
            url = f"data:text/css;base64,{fb_base64}"
        elif fb[1] in plain_text:
            url = f"data:text/plain;base64,{fb_base64}"
        else:
            st.error("不支持这种格式")
            return [],[]
        if isimage:
            files_base64.append({
                "type": "image_url",
                "image_url": {
                    "url": url,
                }
            })
            f_file.append({
                "type": "image_url",
                "image_url": {
                    "url": fb[2],
                }
            })
        else:
            files_base64.append({
                "type": "file",
                "file_url": {
                    "url": url,
                }
            })
            f_file.append({
                "type": "file",
                "file_url": {
                    "url": fb[0],
                }
            })
    return files_base64,f_file


def generate_response(message_history,use_search):
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    payload = {
        "model":model,
        "messages": message_history,
        "use_search":use_search,
        "stream":False
    }
    with requests.post(base_url, json=payload, headers=headers) as response:
        data = json.loads(response.content)["choices"][0]["message"]["content"]
    return data


def show_chat_history(message_history):
    for message in message_history:
        if message["role"] == "user":
            with show_chat.chat_message("user"):
                if isinstance( message["content"],list):
                    for content in message["content"]:
                        if content["type"] == "text":
                            st.write(content["text"])
                        if content["type"] == "image_url":
                            st.image(content["image_url"]["url"])
                        if content["type"] == "file":
                            with st.container(border=True):
                                st.write(content["file_url"]["url"])
                else:
                    st.write(message["content"])

        else:
            with show_chat.chat_message(model):
                st.write(message["content"])


def change_use_search():
    st.session_state.use_search = not st.session_state.use_search


with st.sidebar:
    st.session_state.use_search = st.toggle("**Offine || Online**",value=st.session_state.use_search,on_change=change_use_search)
    st.session_state.file_upload = st.file_uploader("**Upload Files**",accept_multiple_files=True,label_visibility="visible")
    st.button("**Clear Chat**",key="ClearChat",use_container_width=True)
    st.button("**ShowChat**",key="ShowChat",use_container_width=True,type="primary")
    if st.session_state.get("ClearChat"):
        st.session_state.message_history = []
        st.session_state.message_history_init = []
    if st.session_state.get("ShowChat"):
        show_chat_history(st.session_state.message_history_init)

prompt = st.chat_input("Send your message")
if prompt:
    message = {
            "role": "user",
            "content": [
                {
                    "type":"text",
                    "text":prompt
                },
            ]
        }
    message_init = {
            "role": "user",
            "content": [
                {
                    "type":"text",
                    "text":prompt
                },
            ]
        }
    if st.session_state.file_upload:
        fb_base64,fb = process_files(st.session_state.file_upload)
        message["content"] += fb_base64
        message_init["content"] += fb
        st.session_state.use_search = False
    st.session_state.message_history.append(message)
    st.session_state.message_history_init.append(message_init)
    print(st.session_state.message_history)
    show_chat_history(st.session_state.message_history_init)

    response = generate_response(st.session_state.message_history,st.session_state.use_search)
    st.session_state.message_history.append({"role": model,"content":response})
    st.session_state.message_history_init.append({"role": model,"content":response})
    with show_chat:
        with st.chat_message(model):
            st.write(response)

@Vinlic Vinlic closed this as completed Apr 12, 2024
@Vinlic Vinlic pinned this issue Apr 12, 2024
@Vinlic Vinlic unpinned this issue May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants