-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
195 lines (147 loc) · 5.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import json
from typing import List
from huggingface_hub import InferenceClient
from fastapi import FastAPI, status, Request
from pydantic import BaseModel, Json
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from dotenv import load_dotenv
import os, uvicorn
import discord, asyncio
from fastapi.middleware.cors import CORSMiddleware
load_dotenv()
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
token = os.getenv("DISCORD")
client = InferenceClient(api_key=os.getenv("TOKEN"))
c_id = 1298667987787845727
class DiscordBot(discord.Client):
async def on_ready(self):
print(f"logged in as {self.user}")
bot = DiscordBot(intents=discord.Intents.default())
print(bot.get_channel(c_id))
def addToDB(obj):
data = None
with open("db.json", "r") as f:
data = json.load(f)
data["orders"].append(obj)
with open("db.json", "w") as f:
json.dump(data, f, indent=4)
async def orderTooJson(convo):
inp = convo
con = [{"role": "system", "content": "Your job is to turn a string of messages between a fast food worker and a customer into a JSON object with this format {foods: [{name: \"\", amount:int}], drinks: []} only answer with the object, do not even put it in a code block or include any message, that is very important. here is the conversation: " + str(convo)}, {"role": "user", "content": ""}]
obj = (
(
client.chat.completions.create(
model="Qwen/Qwen2.5-72B-Instruct",
messages=con,
temperature=0.5,
max_tokens=1024,
top_p=0.7,
)
)
.choices[0]
.message
)
js = json.loads(obj.content)
addToDB(js)
channel = bot.get_channel(c_id)
await channel.send(js)
async def newAIResp(convo):
inp = convo
return (
(
client.chat.completions.create(
model="Qwen/Qwen2.5-72B-Instruct",
messages=inp,
temperature=0.5,
max_tokens=1024,
top_p=0.7,
)
)
.choices[0]
.message
)
async def makeAIResp(msg):
inp = [
{
"role": "system",
"content": 'You are a fast food employee, Your job is to accept orders, and convert them into a json schema like this: "{foods: [], drinks: [], others: []}", where foods is food items and other things you eat, and drinks is things you drink, others is anything else. Respond with JUST a single json object, do not surround it in a code block or anything else. Do not add any items that do not belong in a fast food menu. To make myself clear, I only want you to put all the items in one object as a list. Make sure to include a key even if it is empty, so the object always has all three keys included.',
},
{"role": "user", "content": msg},
]
return (
(
client.chat.completions.create(
model="mistralai/Mistral-7B-Instruct-v0.3",
messages=inp,
temperature=0.5,
max_tokens=1024,
top_p=0.7,
)
)
.choices[0]
.message
)
class Order(BaseModel):
foods: List
drinks: List
class OrderDB(Order):
method: str
class Message(BaseModel):
role: str
content: str
class AIMessages(BaseModel):
messages: List[Message]
@app.post("/basic", status_code=status.HTTP_204_NO_CONTENT)
async def basic(order: Order):
item = OrderDB(method="basic", **order.model_dump())
print(item.model_dump())
addToDB(item.model_dump())
channel = bot.get_channel(c_id)
await channel.send(str(item.model_dump()))
return None
# ai assistant conversation lol
@app.post("/convo")
async def conversation(order: AIMessages):
m = [i.model_dump() for i in order.messages]
out = await newAIResp(m)
c = 0
if "yippee" in out.content.lower():
c = 1
await orderTooJson(m)
# return {"message": "Your order was complted!", "cont": 1}
return {"message": out.content, "cont": c}
@app.post("/ai")
async def ai(order):
# out = await makeAIResp(order.message)
# print(out)
# data = json.load(out.content)
# item = OrderDB(
# method="AI", foods=data["foods"], drinks=data["drinks"], others=data["others"]
# )
return None # out
@app.get("/app")
async def appHome(request: Request):
return templates.TemplateResponse(request=request, name="home.html")
@app.get("/app/ai")
async def aiUI(request: Request):
return templates.TemplateResponse(request=request, name="ai.html")
@app.get("/app/basic")
async def basicUI(request: Request):
return templates.TemplateResponse(request=request, name="basic.html", context={})
async def run_fastapi():
config = uvicorn.Config(app, host="0.0.0.0", port=8000)
server = uvicorn.Server(config)
await server.serve()
# Main function to run both FastAPI and Discord bot concurrently
async def main():
# Run FastAPI and Discord bot concurrently
await asyncio.gather(
bot.start(token),
run_fastapi()
)
await bot.start(token)
if __name__ == "__main__":
asyncio.run(main())