GithubHelp home page GithubHelp logo

reka's Introduction

使用方法

拉取本项目

git clone https://github.com/jessfin/reka
cd reka
pip install -r requirements.txt
python3 main.py

reka's People

Contributors

jessfin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

reka's Issues

修复结尾会出现<seq的bug,如下为修改后的main.py内容

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.