Spaces:
Runtime error
Runtime error
Kastg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,36 @@
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, HTTPException, Request
|
2 |
from fastapi.responses import JSONResponse
|
3 |
-
from llama_cpp import Llama
|
4 |
import gradio as gr
|
5 |
|
|
|
6 |
app = FastAPI()
|
7 |
|
|
|
8 |
llm = gr.Llama(model_path="model.gguf", n_ctx=4000, n_threads=2, chat_format="chatml")
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
@app.post("/api/v1/chat")
|
11 |
async def chat_post(request: Request):
|
|
|
|
|
|
|
|
|
12 |
data = await request.json()
|
13 |
message = data.get("message")
|
14 |
history = data.get("history", [])
|
@@ -18,23 +40,31 @@ async def chat_post(request: Request):
|
|
18 |
async def generate():
|
19 |
system_prompt = "You are OpenChat, a useful AI assistant."
|
20 |
formatted_prompt = [{"role": "system", "content": system_prompt}]
|
21 |
-
for user_prompt, bot_response
|
22 |
formatted_prompt.append({"role": "user", "content": user_prompt})
|
23 |
-
formatted_prompt.append({"role": "assistant", "content": bot_response
|
24 |
formatted_prompt.append({"role": "user", "content": message})
|
25 |
-
stream_response = llm.create_chat_completion(messages=formatted_prompt, temperature=temperature,
|
26 |
-
|
|
|
27 |
for chunk in stream_response:
|
28 |
if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
|
29 |
-
response
|
30 |
yield response
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
|
|
34 |
@app.get("/api/v1/chat")
|
35 |
async def chat_get():
|
36 |
return {"message": "Send a POST request to this endpoint to chat."}
|
37 |
|
|
|
38 |
if __name__ == "__main__":
|
39 |
import uvicorn
|
40 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
1 |
+
import discord
|
2 |
+
from discord.ext import commands
|
3 |
+
from discord.ext.commands import Context
|
4 |
from fastapi import FastAPI, HTTPException, Request
|
5 |
from fastapi.responses import JSONResponse
|
|
|
6 |
import gradio as gr
|
7 |
|
8 |
+
# Initialize FastAPI app
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Initialize Gradio Llama model
|
12 |
llm = gr.Llama(model_path="model.gguf", n_ctx=4000, n_threads=2, chat_format="chatml")
|
13 |
|
14 |
+
# Initialize Discord bot
|
15 |
+
bot = commands.Bot(command_prefix='&') # Define the command prefix
|
16 |
+
|
17 |
+
# Global variable to store the channel where chats will be sent
|
18 |
+
chat_channel = None
|
19 |
+
|
20 |
+
# Define the command to set the chat channel
|
21 |
+
@bot.command()
|
22 |
+
async def set_channel(ctx: Context, channel: discord.TextChannel):
|
23 |
+
global chat_channel
|
24 |
+
chat_channel = channel
|
25 |
+
await ctx.send(f"Chat channel set to {channel.mention}")
|
26 |
+
|
27 |
+
# Define the function to handle the chat endpoint
|
28 |
@app.post("/api/v1/chat")
|
29 |
async def chat_post(request: Request):
|
30 |
+
global chat_channel
|
31 |
+
if chat_channel is None:
|
32 |
+
raise HTTPException(status_code=400, detail="Chat channel is not set")
|
33 |
+
|
34 |
data = await request.json()
|
35 |
message = data.get("message")
|
36 |
history = data.get("history", [])
|
|
|
40 |
async def generate():
|
41 |
system_prompt = "You are OpenChat, a useful AI assistant."
|
42 |
formatted_prompt = [{"role": "system", "content": system_prompt}]
|
43 |
+
for user_prompt, bot_response in history:
|
44 |
formatted_prompt.append({"role": "user", "content": user_prompt})
|
45 |
+
formatted_prompt.append({"role": "assistant", "content": bot_response})
|
46 |
formatted_prompt.append({"role": "user", "content": message})
|
47 |
+
stream_response = llm.create_chat_completion(messages=formatted_prompt, temperature=temperature,
|
48 |
+
max_tokens=max_tokens, stream=True)
|
49 |
+
response = ""
|
50 |
for chunk in stream_response:
|
51 |
if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
|
52 |
+
response += chunk['choices'][0]["delta"]["content"]
|
53 |
yield response
|
54 |
|
55 |
+
# Send the generated response to the chat channel
|
56 |
+
async for response in generate():
|
57 |
+
await chat_channel.send(response)
|
58 |
+
|
59 |
+
return JSONResponse(content={"response": "Message sent to chat channel"})
|
60 |
|
61 |
+
# Define the function to handle the GET request for chat
|
62 |
@app.get("/api/v1/chat")
|
63 |
async def chat_get():
|
64 |
return {"message": "Send a POST request to this endpoint to chat."}
|
65 |
|
66 |
+
# Run the bot
|
67 |
if __name__ == "__main__":
|
68 |
import uvicorn
|
69 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
70 |
+
bot.run('YOUR_DISCORD_BOT_TOKEN') # Replace 'YOUR_DISCORD_BOT_TOKEN' with your actual bot token
|