Using openai.com API

Has anyone successfully connected to the openai API from Solar2d?

I am trying to connect to the openai.com API, and going in circles. There is lua code to connect, but it is designed for use directly from lua on a computer with openai installed. I have downloaded the openai code but am having trouble getting it to work – I seem to be stuck on the ssl part (can’t find ssl.core). Here is what I have done.

  1. downloaded the init.lua from openai lua API code: GitHub - leafo/lua-openai: OpenAI API bindings for Lua
    → can’t find cjson: I just substituted json
    → can’t find tableshape
  2. download tableshape.lua and the init etc: GitHub - leafo/tableshape: Test the shape or structure of a Lua table, inspired by React.PropTypes & LPeg
    → can’t find ssl
  3. downloaded ssl files: gmod_luasec/includes/modules/ssl.https.lua at master · danielga/gmod_luasec · GitHub
    Included ssl.lua, ssl.https.lua,ssl.x509.lua,ssl.context.lua,ssl.htttps.co.lua. I put them in an ssl subdirectory (removing ssl from the name) so it could find them, and it did.
    → can’t find ssl.core

How do I get out of this, or reference ssl.core? Supposed to be bundled into ssl, so I am a bit of a loss.

Never mind. I just used the simple REST api and it all works perfectly.

	local packet = {
		["Content-Type"] = "application/json",
		["Authorization"] = "Bearer "..oaikey
	}
	local body = {
		model = "gpt-4o-mini",
     	messages = {{role = "user", content = "suggest 3 "..C.COST[cost].." dinner restaurants within walking distance of Russell Square London and the walking time to get there"}},
     	temperature = 0.7
	}
	local url = "https://api.openai.com/v1/chat/completions"

2 Likes