Подключаем CHAT GPT 3.5 Turbo в SAMP

JasonEllison

Известный
Автор темы
5
4
Доброго времени суток.
Видел тему - https://www.blast.hk/threads/164336/
Но версия AI там устаревшая, может кому нужно, подключение GPT 3.5 Turbo, Как основа для Ваших скриптов) ботов и помошников.​
Подача запроса AI:
imgui.InputTextMultiline(u8 "##138", input_news_buf, ffi.sizeof(input_news_buf), imgui.ImVec2(785, 175))
if imgui.Button(u8'A1', imgui.ImVec2(40, 24)) then
    local input_text = u8:decode(ffi.string(input_news_buf))
    onSendChat(input_text)
end
В вводим Ваш запрос, кнопочкой отправляем.
Запрос:
function onSendChat(message)
    OpenAI_Generate(message, false, function(responseText)
        sampAddChatMessage("Ответ GPT-3.5: " .. responseText, 0xFFFFFF)
    end)
end

function OpenAI_Generate(prompt, dontFreeze, customCallback)
    asyncHttpRequest('POST', 'https://api.openai.com/v1/chat/completions', {
            headers = {
                ['Authorization'] = 'Bearer ' .. 'YOU API KEY',
                ['Content-Type'] = 'application/json'
            },
            data = u8(encodeJson({
                ["model"] = "gpt-3.5-turbo",
                ["messages"] = {
                    {
                        ["role"] = "system",
                        ["content"] = "You are a helpful assistant."
                    },
                    {
                        ["role"] = "user",
                        ["content"] = prompt
                    }
                },
                ["temperature"] = 0.5,
                ["max_tokens"] = 50,
                ["top_p"] = 1,
                ["frequency_penalty"] = 0,
                ["presence_penalty"] = 0,
                ["stop"] = {"You:"}
            }))
        },
        function(response)
            if response.status_code == 200 then
                local RESULT = decodeJson(response.text)
                if RESULT.choices and #RESULT.choices > 0 then
                    local text = RESULT.choices[1].message.content
                    if text ~= nil then
                        text = u8:decode(text)
                        if customCallback then
                            customCallback(text)
                        else
                            sendChatMessage("GPT-3.5 ответ: " .. text)
                        end
                    else
                        print("Ошибка: текст ответа равен nil")
                    end
                else
                    print("Ошибка: сервер не вернул ни одного варианта ответа")
                end
            else
                print("Ошибка: HTTP код: " .. response.status_code)
                print("Текст ошибки: " .. response.text)
            end
        end,
        function(err)
            print("Ошибка: " .. err)
        end
    )
end

function asyncHttpRequest(method, url, args, resolve, reject)
    local request_thread = effil.thread(function (method, url, args)
        local requests = require 'requests'
        local result, response = pcall(requests.request, method, url, args)
        if result then
            response.json, response.xml = nil, nil
            return true, response
        else
            return false, response
        end
    end)(method, url, args)
    -- Если запрос без функций обработки ответа и ошибок.
    if not resolve then resolve = function() end end
    if not reject then reject = function() end end
    -- Проверка выполнения потока
    lua_thread.create(function()
        local runner = request_thread
        while true do
            local status, err = runner:status()
            if not err then
                if status == 'completed' then
                    local result, response = runner:get()
                    if result then
                        resolve(response)
                    else
                        reject(response)
                    end
                    return
                elseif status == 'canceled' then
                    return reject(status)
                end
            else
                return reject(err)
            end
            wait(0)
        end
    end)
end

В коде где YOU API KEY, вместо этого вставляем ВАШ API KEy
https://platform.openai.com/account/api-keys
После чего, чат-бот на версии CHATGPT 3.5 Turbo будет у Вас в игре :)
 

diva

Известный
248
44
I did that but nothing actually showed up?

1679739672036.png
 

RADMIR CHEAT 2.0

Потрачен
78
8
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
imgui.InputTextMultiline(u8 "##138", input_news_buf, ffi.sizeof(input_news_buf), imgui.ImVec2(785, 175))
if imgui.Button(u8'A1', imgui.ImVec2(40, 24)) then
local input_text = u8:decode(ffi.string(input_news_buf))
onSendChat(input_text)
end
Куда вставлять?