20 Commity 9265b641de ... 751cd5db5f

Autor SHA1 Wiadomość Data
  Andrew Whatson 751cd5db5f Bump version to 0.2 1 rok temu
  Andrew Whatson dabfa4eba5 Document the autotools build 1 rok temu
  Andrew Whatson 9a9fa57ee1 Document openai-text-edit and openai-code-edit 1 rok temu
  Andrew Whatson 77753b17c8 Minor documentation fixes 1 rok temu
  Andrew Whatson 602042ccbc Update image-edit docs, use opaque mask by default 1 rok temu
  Andrew Whatson 45e303b91c Tidy up magick FFI wrapper naming 1 rok temu
  Andrew Whatson d50a966736 Default to a fully-transparent edit mask 1 rok temu
  Andrew Whatson d271264431 Use ImageMagick to process generated images 1 rok temu
  Andrew Whatson 2e9cc8b418 Review foreign types, keyword args, function wrappers 1 rok temu
  Andrew Whatson 19c630405d Add autotools build system 1 rok temu
  Andrew Whatson 4893b1260b Implement wand-error return type 1 rok temu
  Andrew Whatson ecca95359a Add magick-convert-image & error handling 1 rok temu
  Andrew Whatson 78aca33506 Export c-type routines & minor cleanup 1 rok temu
  Andrew Whatson 8ca1f32a51 Add prototype image-magick FFI 1 rok temu
  Andrew Whatson c2413de182 Add openai-image-edit 1 rok temu
  Andrew Whatson 0a872eacee Add openai-moderation 1 rok temu
  Andrew Whatson e3d2ba138b Fix openai-embedding args being optional instead of keyword 1 rok temu
  Andrew Whatson e9097f1632 Adjust fill-column and re-wrap README 1 rok temu
  Andrew Whatson f62de63b15 Implement moderation API 1 rok temu
  Andrew Whatson 540137147f Mention gpt-4-32k in chat docs 1 rok temu
10 zmienionych plików z 283 dodań i 34 usunięć
  1. 1 1
      .dir-locals.el
  2. 1 0
      .envrc
  3. 18 0
      .gitignore
  4. 27 0
      Makefile.am
  5. 178 24
      README.md
  6. 3 0
      bootstrap
  7. 22 0
      build-aux/guile-rules.am
  8. 24 0
      configure.ac
  9. 9 9
      guix.scm
  10. 0 0
      openai.scm

+ 1 - 1
.dir-locals.el

@@ -1,5 +1,5 @@
 ((nil
-  (fill-column . 72)
+  (fill-column . 70)
   (tab-width   .  8)
   (sentence-end-double-space . t))
  (scheme-mode

+ 1 - 0
.envrc

@@ -1,2 +1,3 @@
 eval $(guix shell guile -Df guix.scm --search-paths)
 path_add GUILE_LOAD_PATH .
+path_add GUILE_LOAD_COMPILED_PATH .

+ 18 - 0
.gitignore

@@ -0,0 +1,18 @@
+# files from bootstrap
+/Makefile.in
+/aclocal.m4
+/autom4te.cache/
+/build-aux/install-sh
+/build-aux/missing
+/configure
+/configure~
+
+# files from configure
+/Makefile
+/config.log
+/config.status
+/openai/config.scm
+/pre-inst-env
+
+# files from make
+*.go

+ 27 - 0
Makefile.am

@@ -0,0 +1,27 @@
+include $(top_srcdir)/build-aux/guile-rules.am
+
+SOURCES = \
+  openai.scm \
+  openai/chat.scm \
+  openai/client.scm \
+  openai/config.scm \
+  openai/completion.scm \
+  openai/debug.scm \
+  openai/edit.scm \
+  openai/embedding.scm \
+  openai/image.scm \
+  openai/moderation.scm \
+  openai/api/chat.scm \
+  openai/api/completion.scm \
+  openai/api/edit.scm \
+  openai/api/embedding.scm \
+  openai/api/image.scm \
+  openai/api/model.scm \
+  openai/api/moderation.scm \
+  openai/utils/colorized.scm \
+  openai/utils/event-stream.scm \
+  openai/utils/foreign.scm \
+  openai/utils/json.scm \
+  openai/utils/magick.scm \
+  openai/utils/multipart.scm \
+  openai/utils/uri.scm

+ 178 - 24
README.md

@@ -2,24 +2,38 @@
 
 This is a [GNU Guile][guile] client for the [OpenAI API][openai-api].
 It currently implements the Chat and Image APIs, with support for chat
-response streaming and inline image display in a [Geiser][geiser] REPL.
+response streaming and inline image display in a [Geiser][geiser]
+REPL.
 
 ## Installation
 
-A [GNU Guix][guix] package definition is provided in the repository, so
-you can install it into your profile with the following:
+### Guix
+
+An up-to-date [GNU Guix][guix] package definition is provided in the
+repository, so you can install it into your profile with the
+following:
 
 ```shell
 guix package -f path/to/guile-openai/guix.scm
 ```
 
-Alternatively, you can jump straight into an interactive environment by
-loading it with [Guile Studio][guile-studio]:
+Alternatively, you can jump straight into an interactive environment
+by loading it with [Guile Studio][guile-studio]:
 
 ```shell
 guix shell guile guile-studio -f path/to/guile-openai/guix.scm -- guile-studio
 ```
 
+### Autotools
+
+A conventional build system is also provided:
+
+``` shell
+cd path/to/guile-openai
+./bootstrap
+./configure && make
+```
+
 ## Documentation
 
 ### Quick start
@@ -110,8 +124,8 @@ ultimately the OpenAI API is a paid service.
   Send a chat completion request.  Returns a *chat* record.
 
   The *prompt* can be a string, which will be sent as a user message.
-  Alternatively, prompt can be a list of `(role . content)` pairs, where
-  content is a string and role is a symbol `system`, `user`, or
+  Alternatively, prompt can be a list of `(role . content)` pairs,
+  where content is a string and role is a symbol `system`, `user`, or
   `assistant`.
 
   The keyword arguments correspond to the request parameters described
@@ -129,7 +143,7 @@ ultimately the OpenAI API is a paid service.
 
     A symbol or string identifying the model to use.  Defaults to
     `gpt-3.5-turbo`, but if you're lucky you might be able to use
-    `gpt-4` here.
+    `gpt-4` or `gpt-4-32k` here.
 
   - `#:temperature`
 
@@ -191,27 +205,54 @@ ultimately the OpenAI API is a paid service.
 
   - `#:size`
 
-    A number specifying the generated image size, either 256, 512, or 1024.
-    The symbols `256x256`, `512x512` and `1024x1024` are also supported.
+    A number specifying the generated image size, either 256, 512,
+    or 1024.  The symbols `256x256`, `512x512` and `1024x1024` are
+    also supported.
 
   - `#:format`
 
-    How the image data will be returned, either `url` (hosted on a cloud
-    server), or `b64` (embedded in the response message, the default).
-    In either case, the image data will be fetched and stored locally
-    for display.
+    How the image data will be returned, either `url` (hosted on a
+    cloud server), or `b64` (embedded in the response message, the
+    default).  In either case, the image data will be fetched and
+    stored locally for display.
 
   - `#:user`
 
     An optional username to associate with this request.
 
+- (**openai-image-edit** image prompt *[keyword arguments]*)
+
+  Send an image edit request.  Returns an *image* record.
+
+  The *image* can be an *image* record or a string path to the image
+  file to be varied.  The API requires the image to be a valid PNG
+  file, less than 4MB, and square.
+
+  The *prompt** must be a string describing the changes to be made to
+  the image.
+
+  The keyword arguments correspond to the request parameters described
+  in the [image edit request][image-edit-request] documentation:
+
+  - `#:mask`
+
+  An *image* record or a string path to an image file to use as the
+  edit mask.  The model will only edit pixels which are fully
+  transparent (ie. alpha 0) in the mask image.  The mask must have the
+  same dimensions as IMAGE.  The default is fully opaque, so no pixels
+  will be edited and a copy of the original image will be returned.
+
+  - `#:n`, `#:size`, `#:format`, `#:user`
+
+    See `openai-image`.
+
 - (**openai-image-variation** image *[keyword arguments]*)
 
   Send an image variation request.  Returns an *image* record.
 
   The *image* can be an *image* record or a string path to the image
-  file to be varied.  The API requires the image to be a valid PNG file,
-  less than 4MB, and square.
+  file to be varied.  The API requires the image to be a valid PNG
+  file, less than 4MB, and square.
 
   The keyword arguments correspond to the request parameters described
   in the [image variation request][image-variation-request]
@@ -273,22 +314,130 @@ ultimately the OpenAI API is a paid service.
 
   The embedding vector generated by the model.
 
+### Text Edit API
+
+- (**openai-text-edit** input instruction *[keyword arguments]*)
+
+  Send an edit request.  Returns an *edit* record.
+
+  The *input* must be a string, which is the text to be edited.  The
+  *instruction* is a string describing the changes to be made.
+
+  The keyword arguments correspond to the request parameters described
+  in the [edit request][edit-request] documentation:
+
+  - `#:model`
+
+    A symbol or string identifying the model to use.  Defaults to
+    `text-davinci-edit-001`.
+
+  - `#:n`
+
+    The number of responses to generate, returned as multiple values.
+
+  - `#:temperature`
+
+    The sampling temperature to use, a number between 0 and 2.
+
+  - `#:top-p`
+
+    An alternative sampling parameter, a number between 0 and 1.
+
+- (**openai-code-edit** input instruction *[keyword arguments]*)
+
+  Identical to `openai-text-edit`, except it defaults to the
+  `code-davinci-edit-001` model.
+
+- (**openai-default-text-edit-model** 'text-davinci-edit-001)
+
+  Parameter for the default `#:model` value (text edit).
+
+- (**openai-default-text-edit-temperature** *unspecified*)
+
+  Parameter for the default `#:temperature` value (text edit).
+
+- (**openai-default-text-edit-top-p** *unspecified*)
+
+  Parameter for the default `#:top-p` value (text edit).
+
+- (**openai-default-code-edit-model** 'code-davinci-edit-001)
+
+  Parameter for the default `#:model` value (code edit).
+
+- (**openai-default-code-edit-temperature** *unspecified*)
+
+  Parameter for the default `#:temperature` value (code edit).
+
+- (**openai-default-code-edit-top-p** *unspecified*)
+
+  Parameter for the default `#:top-p` value (code edit).
+
+- (**edit?** edit)
+
+  Type predicate for edit responses.
+
+- (**edit-content** edit)
+
+  The edited content generated by the model.
+
+### Moderation API
+
+- (**openai-moderation** input *[keyword arguments]*)
+
+  Send a moderation request.  Returns a *moderation* record.
+
+  The *input* must be a string to be classified by the moderation
+  model.
+
+  The keyword arguments correspond to the request parameters described
+  in the [moderation request][moderation-request] documentation:
+
+  - `#:model`
+
+    A symbol or string identifying the model to use.  The default is
+    `*unspecified*`, which is equivalent to `text-moderation-latest`.
+
+- (**openai-default-moderation-model** *unspecified*)
+
+  Parameter for the default `#:model value`.
+
+- (**moderation?** moderation)
+
+  Type predicate for moderation responses.
+
+- (**moderation-flagged?** moderation)
+
+  Whether the content was in violation of OpenAI's content policy.
+
+- (**moderation-categories** moderation)
+
+  The list of categories which caused this content to be flagged.
+
+- (**moderation-scores** moderation)
+
+  An association list of the scores for this content in each category.
+
+- (**moderation-score** moderation category)
+
+  The moderation score for this content in the specified category.
+
 ## License
 
 Copyright © 2023 Andrew Whatson <whatson@tailcall.au>
 
-guile-openai is free software: you can redistribute it and/or modify it
-under the terms of the GNU Affero General Public License as published by
-the Free Software Foundation, either version 3 of the License, or (at
-your option) any later version.
+guile-openai is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
 
 guile-openai is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
-General Public License for more details.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Affero General Public License for more details.
 
-You should have received a copy of the GNU Affero General Public License
-along with guile-openai.  If not, see <https://www.gnu.org/licenses/>.
+You should have received a copy of the GNU Affero General Public
+License along with guile-openai.  If not, see
+<https://www.gnu.org/licenses/>.
 
 See [COPYING](COPYING) for details.
 
@@ -299,5 +448,10 @@ See [COPYING](COPYING) for details.
 [guix]: https://guix.gnu.org/
 [chat-request]: https://platform.openai.com/docs/api-reference/chat/create
 [image-request]: https://platform.openai.com/docs/api-reference/images/create
+[image-edit-request]:
+https://platform.openai.com/docs/api-reference/images/create-edit
 [image-variation-request]: https://platform.openai.com/docs/api-reference/images/create-variation
 [embedding-request]: https://platform.openai.com/docs/api-reference/embeddings
+[moderation-request]: https://platform.openai.com/docs/api-reference/moderations
+[edit-request]:
+https://platform.openai.com/docs/api-reference/edits

+ 3 - 0
bootstrap

@@ -0,0 +1,3 @@
+#!/bin/sh
+
+autoreconf -vif

+ 22 - 0
build-aux/guile-rules.am

@@ -0,0 +1,22 @@
+GOBJECTS = $(SOURCES:%.scm=%.go)
+
+nobase_mod_DATA = $(SOURCES) $(NOCOMP_SOURCES)
+nobase_go_DATA = $(GOBJECTS)
+
+# Make sure source files are installed first, so that the mtime of
+# installed compiled files is greater than that of installed source
+# files.  See
+# <http://lists.gnu.org/archive/html/guile-devel/2010-07/msg00125.html>
+# for details.
+guile_install_go_files = install-nobase_goDATA
+$(guile_install_go_files): install-nobase_modDATA
+
+CLEANFILES = $(GOBJECTS)
+EXTRA_DIST = $(SOURCES) $(NOCOMP_SOURCES)
+GUILE_WARNINGS = -Wunbound-variable -Warity-mismatch -Wformat
+SUFFIXES = .scm .go
+.scm.go:
+	$(AM_V_GEN)$(top_builddir)/pre-inst-env $(GUILE_TOOLS) compile $(GUILE_WARNINGS) -o "$@" "$<"
+
+moddir=$(prefix)/share/guile/site/$(GUILE_EFFECTIVE_VERSION)
+godir=$(libdir)/guile/$(GUILE_EFFECTIVE_VERSION)/site-ccache

+ 24 - 0
configure.ac

@@ -0,0 +1,24 @@
+AC_INIT(guile-openai, 0.2)
+AC_CONFIG_SRCDIR([openai])
+AC_CONFIG_AUX_DIR([build-aux])
+AM_INIT_AUTOMAKE([color-tests -Wall -Wno-portability foreign])
+AM_SILENT_RULES([yes])
+
+AC_PATH_PROG([GUILE], [guile])
+AC_CONFIG_FILES([Makefile])
+AC_CONFIG_FILES([pre-inst-env], [chmod +x pre-inst-env])
+AC_CONFIG_FILES([openai/config.scm])
+
+GUILE_PKG([3.0])
+GUILE_PROGS
+
+GUILE_MODULE_REQUIRED([ice-9 colorized])
+GUILE_MODULE_REQUIRED([gnutls])
+GUILE_MODULE_REQUIRED([json])
+GUILE_MODULE_REQUIRED([pict base64])
+
+PKG_CHECK_MODULES([MAGICKWAND], [MagickWand])
+PKG_CHECK_VAR([MAGICKWAND_LIBDIR], [MagickWand], [libdir])
+PKG_CHECK_VAR([MAGICKWAND_LIBNAME], [MagickWand], [libname])
+
+AC_OUTPUT

+ 9 - 9
guix.scm

@@ -18,13 +18,16 @@
 ;;; <https://www.gnu.org/licenses/>.
 
 (use-modules (guix packages)
-             (guix build-system guile)
+             (guix build-system gnu)
              (guix build utils)
              (guix gexp)
              (guix git-download)
              ((guix licenses) #:prefix license:)
+             (gnu packages autotools)
              (gnu packages guile)
              (gnu packages guile-xyz)
+             (gnu packages imagemagick)
+             (gnu packages pkg-config)
              (gnu packages tls)
              (ice-9 popen)
              (ice-9 textual-ports))
@@ -39,17 +42,14 @@
 (define guile-openai
   (package
    (name "guile-openai")
-   (version (git-version "0.1" "HEAD" %git-commit))
+   (version (git-version "0.2" "HEAD" %git-commit))
    (source (local-file %source-dir
                        #:recursive? #t
                        #:select? (git-predicate %source-dir)))
-   (build-system guile-build-system)
-   (arguments (list #:scheme-file-regexp
-                    #~(lambda (file info)
-                        (let ((name (basename file)))
-                          (and (string-suffix? ".scm" name)
-                               (not (string=? (basename file) "guix.scm")))))))
-   (inputs (list guile-3.0-latest))
+   (build-system gnu-build-system)
+   (arguments (list #:strip-binaries? #f))
+   (native-inputs (list automake autoconf pkg-config))
+   (inputs (list guile-3.0-latest imagemagick))
    (propagated-inputs (list guile-colorized
                             guile-gnutls
                             guile-json-4

+ 0 - 0
openai.scm


Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików