Using OpenAI from Allegro Common Lisp

Written by on January 20, 2025 in LISP, OpenAI, Programming with 0 Comments

Allegro Common Lisp ver 11.0 introduced support for OpenAI LLMs. In this article, let us look at some of the functions for interacting with OpenAI.

First we need to specify basic parameters such as the API key, LLM to use, Temperature, etc. I have defined a convenient function configure-openai to do this (full source is included at the end). So, let us launch the IDE and after loading our source file, execute this function:

Configuring the LLM

Configuring the LLM

I am using the “gpt-4o-mini” model for subsequent interaction.

The function list-openai-models returns a list of the currently supported models:

OpenAI Models

List of OpenAI Models

Let us define a simple function that asks the LLM a Math-related question:

Asking Math Question

Asking Math Question

As you can see, the function calls the built-in function ask-chat, finally returning a numeric value.

Let us see if this works as expected:

Asking Math Questions

Math Questions Examples

That is good! The answers are correct. 

Another built-in function is ask-for-list. This sends a prompt to the LLM and returns the answer in the form of a list. Let us ask the LLM to give us a list of positive and negtive sentiment English words.

Returning List of Values

Returning List of Values

Another function ask-for-map is a variant of the above and it returns a list of key-value pairs. Let us ask for southern states of India and their respective capitals:

Returning Maps

Returning Maps

A further generalization of the above is ask-for-table, which returns multiple values as a table:

Returning a Table of Values

Returning a Table of Values

Here is the complete code that I wrote:

Source Code

Source Code

In addition to directly interacting with the LLM, Allegro CL also supports Vector database and RAG. I will discuss this in a future article.

It is not clear to me why Franz decided to support only OpenAI in this Alegro CL version. It is not difficult to support other vendors such as Anthropic and open-source models. That is a major disappointment for me. Hope they will include this in a future version soon.

Have a nice week!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top