|
| 1 | +from base64 import b64encode |
| 2 | + |
1 | 3 | import streamlit as st
|
2 | 4 | from langchain_community.callbacks.streamlit import (
|
3 | 5 | StreamlitCallbackHandler,
|
|
9 | 11 | )
|
10 | 12 | from template_langgraph.tools.common import get_default_tools
|
11 | 13 |
|
| 14 | + |
| 15 | +def image_to_base64(image_bytes: bytes) -> str: |
| 16 | + return b64encode(image_bytes).decode("utf-8") |
| 17 | + |
| 18 | + |
12 | 19 | if "chat_history" not in st.session_state:
|
13 | 20 | st.session_state["chat_history"] = []
|
14 | 21 |
|
|
43 | 50 |
|
44 | 51 | for msg in st.session_state["chat_history"]:
|
45 | 52 | if isinstance(msg, dict):
|
46 |
| - st.chat_message(msg["role"]).write(msg["content"]) |
| 53 | + attachments = msg.get("attachments", []) |
| 54 | + with st.chat_message(msg["role"]): |
| 55 | + if attachments: |
| 56 | + for item in attachments: |
| 57 | + if item["type"] == "text": |
| 58 | + st.markdown(item["text"]) |
| 59 | + elif item["type"] == "image_url": |
| 60 | + st.image(item["image_url"]["url"]) |
| 61 | + else: |
| 62 | + st.write(msg["content"]) |
47 | 63 | else:
|
48 | 64 | st.chat_message("assistant").write(msg.content)
|
49 | 65 |
|
50 |
| -if prompt := st.chat_input(): |
51 |
| - st.session_state["chat_history"].append({"role": "user", "content": prompt}) |
52 |
| - st.chat_message("user").write(prompt) |
| 66 | +if prompt := st.chat_input( |
| 67 | + accept_file="multiple", |
| 68 | + file_type=[ |
| 69 | + "png", |
| 70 | + "jpg", |
| 71 | + "jpeg", |
| 72 | + "gif", |
| 73 | + "webp", |
| 74 | + ], |
| 75 | +): |
| 76 | + user_display_items = [] |
| 77 | + message_parts = [] |
| 78 | + |
| 79 | + prompt_text = prompt if isinstance(prompt, str) else getattr(prompt, "text", "") or "" |
| 80 | + prompt_files = [] if isinstance(prompt, str) else (getattr(prompt, "files", []) or []) |
| 81 | + |
| 82 | + user_text = prompt_text |
| 83 | + if user_text.strip(): |
| 84 | + user_display_items.append({"type": "text", "text": user_text}) |
| 85 | + message_parts.append(user_text) |
| 86 | + |
| 87 | + has_unsupported_files = False |
| 88 | + for file in prompt_files: |
| 89 | + if file.type and file.type.startswith("image/"): |
| 90 | + image_bytes = file.getvalue() |
| 91 | + base64_image = image_to_base64(image_bytes) |
| 92 | + image_url = f"data:{file.type};base64,{base64_image}" |
| 93 | + user_display_items.append( |
| 94 | + { |
| 95 | + "type": "image_url", |
| 96 | + "image_url": {"url": image_url}, |
| 97 | + } |
| 98 | + ) |
| 99 | + message_parts.append(f"") |
| 100 | + else: |
| 101 | + has_unsupported_files = True |
| 102 | + |
| 103 | + if has_unsupported_files: |
| 104 | + st.warning("画像ファイル以外の添付は現在サポートされていません。") |
| 105 | + |
| 106 | + message_content = "\n\n".join(message_parts).strip() |
| 107 | + if not message_content: |
| 108 | + message_content = "ユーザーが画像をアップロードしました。" |
| 109 | + |
| 110 | + new_user_message = {"role": "user", "content": message_content} |
| 111 | + if user_display_items: |
| 112 | + new_user_message["attachments"] = user_display_items |
| 113 | + |
| 114 | + st.session_state["chat_history"].append(new_user_message) |
| 115 | + |
| 116 | + with st.chat_message("user"): |
| 117 | + if user_display_items: |
| 118 | + for item in user_display_items: |
| 119 | + if item["type"] == "text": |
| 120 | + st.markdown(item["text"]) |
| 121 | + elif item["type"] == "image_url": |
| 122 | + st.image(item["image_url"]["url"]) |
| 123 | + else: |
| 124 | + st.write(message_content) |
| 125 | + |
| 126 | + graph_messages = [] |
| 127 | + for msg in st.session_state["chat_history"]: |
| 128 | + if isinstance(msg, dict): |
| 129 | + graph_messages.append({"role": msg["role"], "content": msg["content"]}) |
| 130 | + else: |
| 131 | + graph_messages.append(msg) |
| 132 | + |
53 | 133 | with st.chat_message("assistant"):
|
54 | 134 | response: AgentState = st.session_state["graph"].invoke(
|
55 |
| - {"messages": st.session_state["chat_history"]}, |
| 135 | + {"messages": graph_messages}, |
56 | 136 | {
|
57 | 137 | "callbacks": [
|
58 | 138 | StreamlitCallbackHandler(st.container()),
|
|
0 commit comments