Git Product home page Git Product logo

prolog2gpt's Introduction

prolog2gpt

SWI Prolog library to interface to the GPT API.

Introduction

Large Language Models (LLM) like GPT have greatly advanced natural language processing in recent years. However, they can benefit from interfacing with other types of reasoning modules, including logic engines (see for example the discussions in "Faithful Chain-of-Thought Reasoning" 2023 by Lyu et al or "FOLIO: Natural Language Reasoning with First-Order Logic" 2022 by Han et al).

Currently, there are interface libraries to GPT for Python and NodeJS, but not Prolog. The work in this repo seeks to address that gap by building a library for SWI Prolog.

Current Status

Pre-alpha: Work has only just started.

Most of the API is working and you can access GPT with simple prolog predicates. Have a look at the code documentation (see below) for examples. Also see the unit tests in src/test/test001.pro

Install

First, make sure your GPT API key is set in the environment variable GPTKEY. Do this:

  1. Create your GPT account at https://platform.openai.com
  2. Create an API key at https://platform.openai.com/account/api-keys and, as instructed, save that key somewhere (e.g. in a text file in a secure folder).
  3. Set an environment variable called GPTKEY to the key value (don't forget in Linux that if you added the environment variable to your bash startup script, e.g. ~/.bashrc, then you need to source your ~/.bashrc script to activate the new environment variable).

Next, there are two ways to install.

Firstly, as a pack from within Prolog, and using it as a library module

:- pack_install('prolog2gpt').
:- use_module(library(prolog2gpt)).

% Now test that the pack installed and execute a first call to GPT
:- init_gptkey.
:- gpt_completions('text-davinci-03','Say hello',Answer,[]).

Otherwise, you can also just clone the git repository.

$ git clone https://github.com/RdR1024/prolog2gpt

Then cd into prolog2gpt/src/prolog and launch, execute swipl and try the following:

:- [prolog2gpt].
:- init_gptkey.  % this makes the gpt key available to the gpt api predicates
:- gpt_completions('text-davinci-003','My favourite animal is ',Text,[max_tokens=30]).

Usage

The prolog predicates mostly follow the GPT API (see https://platform.openai.com/docs/api-reference). However, there is usually a wrapper that returns the results from the API call into something Prolog-friendly (rather than leaving the results as a complex JSON data structure).

For example, gpt_completions(Model,Prompt,Result,Options) will return a Prolog list of generated texts (called "completions" in GPT speak). If you want the returned JSON results instead, you can try gpt_completions(Model,Prompt,Result,Raw,Options) where Raw is set to true. In this case, the Result will be a json term structure. Most of the api predicates have this same "Raw" option.

Repository structure

This repository has the following structure:

  • docs contains literature and additional documentation. A special subdirectory called wiki contains the source files for the separate github wiki repository (github.com:RdR1024/prolog2gpt.wiki.git). Note: I haven't populated this yet.
  • rel contains the periodic releases of the library. Users should use this directory to download stable copies of library.
  • src contains the source code.
    • prolog contains the prolog source code
  • archive old material that we keep for reference
  • test testing files for the source code

Documentation

You can read the individual predicate comments in the source files (e.g. prolog2gpt.pro), or start the SWI-Prolog document server as follows:

:- doc_server(3030).
:- portray_text(true).
:- ['prolog2gpt.pro'].
:- doc_server.

This should launch a web browser with the documentation for the prolog2gpt.pro file.

Known Issues

Three of the APIs don't work. Luckily, they are not critical APIs, because there are workarounds. The problem seems to be with the way the URLs are formulated. They are all URLs that end in /{id}/{instruction} I'm investigating the issues, but so far no luck.
The problem APIs are:

prolog2gpt's People

Contributors

rdr1024 avatar jds-amplify avatar

Stargazers

Paweł Wodnicki avatar bluerxing avatar Weidi Xu avatar Paulo Moura avatar  avatar Jize (Winston) Jiang avatar Marco Craveiro avatar Wenhao Deng avatar  avatar Mikołaj Dorosławski avatar Pierce Brooks avatar  avatar Sam Neaves avatar Hakan Kjellerstrand avatar  avatar Eric Taucher avatar

Watchers

Hakan Kjellerstrand avatar Sam Neaves avatar  avatar Tom Johnson avatar

prolog2gpt's Issues

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.