nomic-ai / gpt4all

GPT4All: Chat with Local LLMs on Any Device
https://gpt4all.io
MIT License
66.87k stars 7.35k forks source link

Python bindings with flask: capturing output #580

Closed mirix closed 1 year ago

mirix commented 1 year ago

Hello,

I have a minimalistic flask app which is already styled and would like to use gpt4all with it.

Here is my code:

from flask import Flask, render_template, request

import gpt4all

gpts = gpt4all.GPT4All('ggml-gpt4all-l13b-snoozy')

app = Flask(__name__)
app.static_folder = 'static'

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/get')
def get_bot_response():
    try: user_input
    except NameError: user_input = ''
    user_input = request.args.get('msg')
    message = [{'role': 'user', 'content': user_input}]

    return gpts.chat_completion(message)

if __name__ == '__main__':
    app.run(host='0.0.0.0') 

This prints the response to the Linux console but the HTML response is:

[object Object]

How do I extract the corresponding string from that object?

Cheers,

Ed

mirix commented 1 year ago

That is the solution (from ChatGPT documentation):

response = gpts.chat_completion(message)['choices'][0]['message']['content']