Giter VIP home page Giter VIP logo

anbuhckr.github.io's Introduction

Chatbot UI

A static web ui chat interface for everyone. base on chatbot-ui

Backend Example with python

# !/usr/bin/env python3
# python3 -m pip install sanic
# python3 -m pip install sanic[ext]
# python3 -m pip install llama-cpp-python

import json, asyncio
from llama_cpp import Llama
from sanic import Sanic, response
from sanic.exceptions import NotFound
from sanic_ext import Extend

model_path = 'Meta-Llama-3-8B.gguf'
api_key = 'lF5CoJ4qHS0Vxypea10IDEHBDaizPKwCGv'
error_msg = {'error': {'type': 'error', 'code': 'error', 'param': 'error', 'message': ''}}

app = Sanic('app')
app.config.CORS_ORIGINS = 'https://anbuhckr.github.io'
Extend(app)

@app.listener('before_server_start')
async def server_init(app, loop):
    app.ctx.llm = Llama(model_path=model_path, verbose=False, n_ctx=4096, seed=1)

@app.listener('after_server_stop')
async def cleanup(*args, **kwargs):
    app.ctx.llm.close()

@app.exception(NotFound)
async def ignore_404s(request, exception):
    error_msg['error']['message'] = 'page not found'
    return response.json(error_msg)

async def chunks(generator):
    for i in generator:
        delta = i['choices'][0]['delta']
        if 'content' in delta or i['choices'][0]['finish_reason'] == 'stop':
            yield f'data: {json.dumps(i)}'
        await asyncio.sleep(.01)

@app.post('/v1/chat/completions')
async def chat(request):
    key = request.headers.get('Authorization', '').split()
    if len(key) != 2 or key[1] != api_key:
        error_msg['error']['message'] = 'api_key not valid'
        return response.json(error_msg)
    res = await request.respond(content_type='text/plain')
    try:
        prompt = {'role': 'system', 'content': request.json.get('prompt')}
        msg = {'role': 'user', 'content': request.json.get('messages')[-1]['content']}
        output = app.ctx.llm.create_chat_completion(
            messages=[prompt, msg],
            temperature=request.json.get('temperature'),
            stream=True,
        )
        async for chunk in chunks(output):
            await res.send(chunk)
    except Exception as e:
        error_msg['error']['message'] = e
        await res.send(json.dumps(error_msg).encode())
    await res.eof()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=False, access_log=False, auto_reload=True)

Add to the ui:

  • api key: lF5CoJ4qHS0Vxypea10IDEHBDaizPKwCGv
  • api host: http://127.0.0.1:8080/v1/chat/completions

Ref

anbuhckr.github.io's People

Contributors

anbuhckr avatar xriley avatar

Watchers

 avatar

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.