Update app.py
Browse files
app.py
CHANGED
|
@@ -31,11 +31,152 @@ generation_args = {
|
|
| 31 |
"do_sample": False,
|
| 32 |
}
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@app.route("/v1/chat/completions", methods=["POST"])
|
| 36 |
def chat_completions():
|
| 37 |
-
data = request.json
|
| 38 |
|
|
|
|
| 39 |
messages = data.get("messages", [])
|
| 40 |
|
| 41 |
result = pipe(messages, **generation_args)
|
|
|
|
| 31 |
"do_sample": False,
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# -----------------------
|
| 35 |
+
# ルートページ (HTML)
|
| 36 |
+
# -----------------------
|
| 37 |
+
@app.route("/")
|
| 38 |
+
def index():
|
| 39 |
+
return """
|
| 40 |
+
<!DOCTYPE html>
|
| 41 |
+
<html>
|
| 42 |
+
<head>
|
| 43 |
+
<meta charset="utf-8">
|
| 44 |
+
<title>Local LLM Chat</title>
|
| 45 |
|
| 46 |
+
<style>
|
| 47 |
+
body{
|
| 48 |
+
font-family: Arial;
|
| 49 |
+
background:#111;
|
| 50 |
+
color:white;
|
| 51 |
+
margin:0;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
#chat{
|
| 55 |
+
height:80vh;
|
| 56 |
+
overflow-y:auto;
|
| 57 |
+
padding:20px;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.message{
|
| 61 |
+
margin-bottom:12px;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
.user{
|
| 65 |
+
color:#6cf;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.assistant{
|
| 69 |
+
color:#9f9;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
#inputArea{
|
| 73 |
+
position:fixed;
|
| 74 |
+
bottom:0;
|
| 75 |
+
width:100%;
|
| 76 |
+
background:#222;
|
| 77 |
+
padding:10px;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
#input{
|
| 81 |
+
width:80%;
|
| 82 |
+
padding:10px;
|
| 83 |
+
font-size:16px;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
button{
|
| 87 |
+
padding:10px;
|
| 88 |
+
font-size:16px;
|
| 89 |
+
}
|
| 90 |
+
</style>
|
| 91 |
+
</head>
|
| 92 |
+
|
| 93 |
+
<body>
|
| 94 |
+
|
| 95 |
+
<h2 style="padding:10px;">Local Phi-3 Chat</h2>
|
| 96 |
+
|
| 97 |
+
<div id="chat"></div>
|
| 98 |
+
|
| 99 |
+
<div id="inputArea">
|
| 100 |
+
<input id="input" placeholder="メッセージを入力..." />
|
| 101 |
+
<button onclick="send()">送信</button>
|
| 102 |
+
</div>
|
| 103 |
+
|
| 104 |
+
<script>
|
| 105 |
+
|
| 106 |
+
let messages = [
|
| 107 |
+
{role:"system",content:"You are a helpful assistant."}
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
function add(role,text){
|
| 111 |
+
|
| 112 |
+
const chat=document.getElementById("chat")
|
| 113 |
+
|
| 114 |
+
const div=document.createElement("div")
|
| 115 |
+
div.className="message "+role
|
| 116 |
+
|
| 117 |
+
div.innerText=role+": "+text
|
| 118 |
+
|
| 119 |
+
chat.appendChild(div)
|
| 120 |
+
chat.scrollTop=chat.scrollHeight
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
async function send(){
|
| 124 |
+
|
| 125 |
+
const input=document.getElementById("input")
|
| 126 |
+
const text=input.value
|
| 127 |
+
|
| 128 |
+
if(!text) return
|
| 129 |
+
|
| 130 |
+
input.value=""
|
| 131 |
+
|
| 132 |
+
add("user",text)
|
| 133 |
+
|
| 134 |
+
messages.push({
|
| 135 |
+
role:"user",
|
| 136 |
+
content:text
|
| 137 |
+
})
|
| 138 |
+
|
| 139 |
+
const res=await fetch("/v1/chat/completions",{
|
| 140 |
+
method:"POST",
|
| 141 |
+
headers:{
|
| 142 |
+
"Content-Type":"application/json"
|
| 143 |
+
},
|
| 144 |
+
body:JSON.stringify({
|
| 145 |
+
messages:messages
|
| 146 |
+
})
|
| 147 |
+
})
|
| 148 |
+
|
| 149 |
+
const data=await res.json()
|
| 150 |
+
|
| 151 |
+
const reply=data.choices[0].message.content
|
| 152 |
+
|
| 153 |
+
add("assistant",reply)
|
| 154 |
+
|
| 155 |
+
messages.push({
|
| 156 |
+
role:"assistant",
|
| 157 |
+
content:reply
|
| 158 |
+
})
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
document.getElementById("input").addEventListener("keypress",function(e){
|
| 162 |
+
if(e.key==="Enter"){
|
| 163 |
+
send()
|
| 164 |
+
}
|
| 165 |
+
})
|
| 166 |
+
|
| 167 |
+
</script>
|
| 168 |
+
|
| 169 |
+
</body>
|
| 170 |
+
</html>
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
# -----------------------
|
| 174 |
+
# OpenAI互換API
|
| 175 |
+
# -----------------------
|
| 176 |
@app.route("/v1/chat/completions", methods=["POST"])
|
| 177 |
def chat_completions():
|
|
|
|
| 178 |
|
| 179 |
+
data = request.json
|
| 180 |
messages = data.get("messages", [])
|
| 181 |
|
| 182 |
result = pipe(messages, **generation_args)
|