assistant / app.py
SamirXR's picture
Update app.py
ad263e7 verified
raw
history blame
2.78 kB
import os
import requests
import json
from io import BytesIO
from quart import Quart, render_template, request, jsonify
import aiohttp
app = Quart(__name__)
# Use a dictionary to store the history for each session
history = {}
@app.route('/')
async def index():
return await render_template('index.html')
@app.route('/generate', methods=['POST'])
async def generate_text():
data = await request.get_json()
prompt = data.get('prompt', '')
session_id = data.get('session_id', 'default')
toggle = data.get('toggle', False)
# Check if the session_id already exists in the history
if session_id not in history:
history[session_id] = [{"role": "system", "content": """
From now on you going to act as Ivan, Keep your answers short and just like a gen-z would. You can also swear and cuss. Just keep in mind that your responses will be from Ivan's perspective.
Here are some dialogue with Ivan:
Oscar: hi Ivan!
Ivan: hii hru
Oscar: im good wbu?
Ivan: same lmao what are you up to?
Oscar: wait i have to go real quick brb, sorry
Ivan: np ur good
As Ivan is a teenager he might come across many abbreviations. Moreover, you have a sarcastic and witty personality, often using extreme humor to express yourself.
"""}]
if toggle:
async with aiohttp.ClientSession() as session:
async with session.get(f'https://ddg-api.awam.repl.co/api/search?query={prompt}') as response:
search_data = await response.json()
search_info = ' '.join([f"Title: {result['Title']}, Link: {result['Link']}, Snippet: {result['Snippet']}" for result in search_data])
# Append the user message to the history for this session
history[session_id].append({"role": "user", "content": prompt})
url = 'https://api.deepinfra.com/v1/openai/chat/completions'
headers = {}
data = {
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"messages": history[session_id],
"max_tokens": 10000,
"stream": False
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data, headers=headers) as response:
response_data = await response.json()
# Extract the assistant's message from the response
if 'choices' in response_data:
assistant_message = response_data['choices'][0]['message']['content']
else:
assistant_message = "I'm sorry, I couldn't generate a response."
# Append the assistant message to the history for this session
history[session_id].append({
"role": "assistant",
"content": assistant_message
})
return jsonify({'result': assistant_message})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)