jessfin / reka Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
from aiohttp import web
import json
import time
import requests
async def fetch(req):
if req.method == "OPTIONS":
return web.Response(body="", headers={'Access-Control-Allow-Origin': '', 'Access-Control-Allow-Headers': ''}, status=204)
# Parse request data
body = await req.json()
messages = body.get("messages", [])
stream = body.get("stream", True)
model_name = body.get("model", "reka-core")
# Construct new request body
conversation_history = []
for message in messages:
role = message.get("role")
content = message.get("content")
if role == "assistant":
conversation_history.append({"type": "model", "text": content})
else:
conversation_history.append({"type": "human", "text": content})
chat_data = {
"conversation_history": conversation_history,
"stream": stream,
"use_search_engine": False,
"use_code_interpreter": False,
"model_name": model_name,
"random_seed": int(time.time())
}
# Processing request data
url = "https://chat.reka.ai/api/chat" # Your URL
token = req.headers.get('Authorization', '') # Get authorization token from request header
headers = {
'Content-Type': 'application/json',
'Authorization': token
} # Set HTTP headers
try:
# Set a timeout of 30 seconds
response = requests.post(url, headers=headers, data=json.dumps(chat_data), stream=True, timeout=30)
except requests.exceptions.Timeout:
print("Request to chat.reka.ai timed out")
return web.Response(status=504) # Gateway Timeout
except ConnectionResetError:
print("Connection reset by peer")
return web.Response(status=500)
except Exception as e:
print(f"Unexpected error: {e}")
return web.Response(status=500) # Internal Server Error
previous_length = 0
response_stream = web.StreamResponse()
response_stream.headers['Content-Type'] = 'text/event-stream'
response_stream.headers['Access-Control-Allow-Origin'] = '*' # Allow cross-origin requests from all origins
response_stream.headers['Access-Control-Allow-Headers'] = '*' # Allow all types of header fields
await response_stream.prepare(req)
writer = response_stream
try:
for line in response.iter_lines():
if line: # Filter out keep-alive new lines
decoded_line = line.decode('utf-8')
if decoded_line.startswith('data:'):
data_content = decoded_line[5:].strip()
data_json = json.loads(data_content)
if data_json['type'] == 'model':
current_text = data_json['text']
# Check if '<sep' is in the text and truncate before it
sep_index = current_text.find('<')
if sep_index != -1:
current_text = current_text[:sep_index]
new_text = current_text[previous_length:]
previous_length = len(current_text)
# Create new JSON object
new_json = {
"id": "chatcmpl-123456789",
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_name,
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": new_text
},
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
}
# Return the new JSON object as part of the stream response, one line at a time
event_data = f"data: {json.dumps(new_json, ensure_ascii=False)}\n\n"
await writer.write(event_data.encode('utf-8'))
except Exception as e:
print(f"Unexpected error while processing response: {e}")
return web.Response(status=500) # Internal Server Error
return writer
async def onRequest(request):
return await fetch(request)
app = web.Application()
app.router.add_route("*", "/v1/chat/completions", onRequest)
if name == 'main':
web.run_app(app, host='0.0.0.0', port=3031)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.