Open AI Client library v0.1.0

I just released an Open AI client library that uses the latest API endpoints. It supports everything except all the endpoints that require a file. I’m not doing much with the fine tunings and audio API’s but I will get around to testing those soon. For now though, the most useful API’s work like a charm. Please give me feedback!

14 Likes

Oh great. I was thinking of contributing to the existing (seemingly abandoned) library once I had some free time, but this is even better!

By the way, what’s currently preventing the endpoints that need a file?

1 Like

@Bonarc

Good question! Just need to figure out the multipart request, I actually have a TODO marked for it:

TODO: This is not working, need to look at HTTP::FormData - Crystal 1.7.3

def multipart_post(path : String, parameters : Hash)
  headers["Content-Type"] = "multipart/form-data"
  response = client.post("/v1" + path, headers: headers, form: parameters.to_json)
  handle_response(response)
end

Once that is complete it can be used across all of the file related endpoints. It shouldn’t be hard too implement but I was going to prioritize implementing streaming with server sent events for the completion endpoints because I need that first! Any help would be appreciated!!

1 Like

This is excellent news! Thanks for the work. Is streaming response supported?

I didn’t have time to implement streaming yet. :frowning: If you have time, I would pull in a PR! @marcstein

Thanks!

Marc

@marcstein I just pushed my initial implementation for streaming. Please give feedback!

Outstanding! Let me check and get back to you.

Thanks!
Marc

1 Like

Just trying the streaming feature and saw something interesting. If I run the following:

response = openai.chat(“gpt-4”, [
{role: “user”, content: “You are a consumer loan officer at a lender. You are evaluating a loan applicant with the following credit file and you need to prepare an extensive report assessing their creditworthiness. Include detailed information about the borrower in the analysis including the FICO score and assigned tier. Close with the offers in the matrix. Present all offers with a DTI of under 40%. LTV must be less than 120%. The bureau used is EXPERIAN. generate response in markdown uuid : #{uuid}, amountFinanced : 8083.90, nmi : 5100, maxLTV : 1.1, maxDTI : 0.4, value : 7349, tier : 6, fico : 607, disputedAccountsExcluded : 000, publicRecordsCount : 001, installmentBalance : 00011102, realEstateBalance : N/A, revolvingBalance : 00004743, pastDueAmount : 00000000, monthlyPayment : 0, realEstatePayment : 0, revolvingAvailablePercent : 042, totalInquiries : 001, inquiriesDuringLast6Months : 001, totalTradeItems : 014, paidAccounts : 007, satisfactoryAccounts : 010, nowDelinquentDerog : 000, wasDelinquentDerog : 004, oldestTradeDate : 09162004, delinquencies30Days : 18, delinquencies60Days : 03, delinquencies90to180Days : 00, derogCounter : 00, dti : 0.0, matrix = Offer 1: Amount Financed: $8,083.00, Term: 48 months, Rate: 22.59%, Monthly Payment: $257.29, DTI: 0.05 Offer 2: Amount Financed: $8,083.00, Term: 60 months, Rate: 22.84%, Monthly Payment: $227.15, DTI: 0.04 Offer 3: Amount Financed: $8,083.00, Term: 72 months, Rate: 23.34%, Monthly Payment: $209.60, DTI: 0.04 Offer 4: Amount Financed: $8,083.00, Term: 84 months, Rate: 23.59%, Monthly Payment: $197.39, DTI: 0.04 Your credit score ranks higher than 16% of consumers.”},
])

@marcstein I didn’t see an output in your response?

Sorry about that!

one shot

OpenAI::ChatResponse(@id=“chatcmpl-7J4jD9es0DLGgYeDBNwaJhgRsFvpb”, @object=“chat.completion”, @created=1684780763, @model=“gpt-4-0314”, @usage=OpenAI::CompletionUsage(@prompt_tokens=545, @total_tokens=1121, @completion_tokens=576), @choices=[{message: {role: “assistant”, content: “# Creditworthiness Assessment Report\n\n## Borrower Information\n- FICO Score: 607\n- Assigned Tier: 6\n- Experian Bureau\n\n## Credit File Summary\n- Disputed Accounts: 0\n- Public Records Count: 1\n- Installment Balance: $11,102\n- Real Estate Balance: N/A\n- Revolving Balance: $4,743\n- Past Due Amount: $0\n- Monthly Payment: $0\n- Real Estate Payment: $0\n- Revolving Available Percent: 42%\n- Total Inquiries: 1\n- Inquiries during last 6 months: 1\n- Total Trade Items: 14\n- Paid Accounts: 7\n- Satisfactory Accounts: 10\n- Now Delinquent Derogatory: 0\n- Was Delinquent Derogatory: 4\n- Oldest Trade Date: September 16, 2004\n- Delinquencies (30 days): 18\n- Delinquencies (60 days): 3\n- Delinquencies (90 to 180 days): 0\n- Derogatory Counter: 0\n- DTI (Debt-to-Income ratio): 0.0\n\n## Loan Offers\nConsidering the provided information and the specified requirements for LTV and DTI, the following loan offers have been provided:\n\n1. Offer 1: \n - Amount Financed: $8,083.00\n - Term: 48 months\n - Rate: 22.59%\n - Monthly Payment: $257.29\n - DTI: 0.05\n\n2. Offer 2: \n - Amount Financed: $8,083.00\n - Term: 60 months\n - Rate: 22.84%\n - Monthly Payment: $227.15\n - DTI: 0.04\n\n3. Offer 3: \n - Amount Financed: $8,083.00\n - Term: 72 months\n - Rate: 23.34%\n - Monthly Payment: $209.60\n - DTI: 0.04\n\n4. Offer 4: \n - Amount Financed: $8,083.00\n - Term: 84 months\n - Rate: 23.59%\n - Monthly Payment: $197.39\n - DTI: 0.04\n\nIt is important to note that with a FICO score of 607, the borrower’s credit score ranks higher than 16% of consumers.”}, finish_reason: “stop”, index: 0}])

streaming

{“role” => “assistant”}
{“content” => “#”}
{“content” => " Credit"}
{“content” => " Analysis"}
{“content” => " Report"}
{“content” => “\n” + “\n”}
{“content” => “###”}
{“content” => " Borrow"}
{“content” => “er”}
{“content” => " Information"}
{“content” => “\n”}
{“content” => “-”}
{“content” => " F"}
{“content” => “ICO”}
{“content” => " Score"}
{“content” => “:”}
{“content” => " “}
{“content” => “607”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Credit”}
{“content” => " Tier"}
{“content” => “:”}
{“content” => " “}
{“content” => “6”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Monthly”}
{“content” => " Income"}
{“content” => “:”}
{“content” => " $“}
{“content” => “5”}
{“content” => “,”}
{“content” => “100”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Dis”}
{“content” => “puted”}
{“content” => " Accounts"}
{“content” => " Ex"}
{“content” => “cluded”}
{“content” => “:”}
{“content” => " “}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Public”}
{“content” => " Records"}
{“content” => " Count"}
{“content” => “:”}
{“content” => " “}
{“content” => “1”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Install”}
{“content” => “ment”}
{“content” => " Balance"}
{“content” => “:”}
{“content” => " $“}
{“content” => “11”}
{“content” => “,”}
{“content” => “102”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Real”}
{“content” => " Estate"}
{“content” => " Balance"}
{“content” => “:”}
{“content” => " N"}
{“content” => “/A”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Rev"}
{“content” => “olving”}
{“content” => " Balance"}
{“content” => “:”}
{“content” => " $“}
{“content” => “4”}
{“content” => “,”}
{“content” => “743”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Past”}
{“content” => " Due"}
{“content” => " Amount"}
{“content” => “:”}
{“content” => " $“}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Monthly”}
{“content” => " Payment"}
{“content” => “:”}
{“content” => " $“}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Real”}
{“content” => " Estate"}
{“content” => " Payment"}
{“content” => “:”}
{“content” => " $“}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Rev”}
{“content” => “olving”}
{“content” => " Available"}
{“content” => " Percent"}
{“content” => “:”}
{“content” => " “}
{“content” => “42”}
{“content” => “%\n”}
{“content” => “-”}
{“content” => " Total”}
{“content” => " In"}
{“content” => “quiries”}
{“content” => “:”}
{“content” => " “}
{“content” => “1”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " In”}
{“content” => “quiries”}
{“content” => " During"}
{“content” => " Last"}
{“content” => " “}
{“content” => “6”}
{“content” => " Months”}
{“content” => “:”}
{“content” => " “}
{“content” => “1”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Total”}
{“content” => " Trade"}
{“content” => " Items"}
{“content” => “:”}
{“content” => " “}
{“content” => “14”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Paid”}
{“content” => " Accounts"}
{“content” => “:”}
{“content” => " “}
{“content” => “7”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " S”}
{“content” => “atisf”}
{“content” => “actory”}
{“content” => " Accounts"}
{“content” => “:”}
{“content” => " “}
{“content” => “10”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Currently”}
{“content” => " Del"}
{“content” => “in”}
{“content” => “quent”}
{“content” => " Der"}
{“content” => “og”}
{“content” => “atory”}
{“content” => " Accounts"}
{“content” => “:”}
{“content” => " “}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Del”}
{“content” => “in”}
{“content” => “quent”}
{“content” => " Der"}
{“content” => “og”}
{“content” => “atory”}
{“content” => " Accounts"}
{“content” => " in"}
{“content” => " the"}
{“content” => " Past"}
{“content” => “:”}
{“content” => " “}
{“content” => “4”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Ol”}
{“content” => “dest”}
{“content” => " Trade"}
{“content” => " Date"}
{“content” => “:”}
{“content” => " “}
{“content” => “09”}
{“content” => “/”}
{“content” => “16”}
{“content” => “/”}
{“content” => “200”}
{“content” => “4”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Del”}
{“content” => “inqu”}
{“content” => “encies”}
{“content” => " (“}
{“content” => “30”}
{“content” => " Days”}
{“content” => “):”}
{“content” => " “}
{“content” => “18”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Del”}
{“content” => “inqu”}
{“content” => “encies”}
{“content” => " (“}
{“content” => “60”}
{“content” => " Days”}
{“content” => “):”}
{“content” => " “}
{“content” => “3”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Del”}
{“content” => “inqu”}
{“content” => “encies”}
{“content” => " (“}
{“content” => “90”}
{“content” => “-”}
{“content” => “180”}
{“content” => " Days”}
{“content” => “):”}
{“content” => " “}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Der”}
{“content” => “og”}
{“content” => “atory”}
{“content” => " Counter"}
{“content” => “:”}
{“content” => " “}
{“content” => “0”}
{“content” => “\n”}
{“content” => “-”}
{“content” => " Debt”}
{“content” => “-to”}
{“content” => “-In”}
{“content” => “come”}
{“content” => " Ratio"}
{“content” => " (“}
{“content” => “DT”}
{“content” => “I”}
{“content” => “):”}
{“content” => " “}
{“content” => “0”}
{“content” => “.”}
{“content” => “0”}
{“content” => “\n” + “\n”}
{“content” => “###”}
{“content” => " Loan”}
{“content” => " Offers”}
{“content” => “\n”}
{“content” => “Please”}
{“content” => " find"}
{“content” => " below"}
{“content” => " the"}
{“content” => " loan"}
{“content” => " offers"}
{“content” => " with"}
{“content” => " a"}
{“content” => " DT"}
{“content” => “I”}
{“content” => " under"}
{“content” => " “}
{“content” => “40”}
{“content” => “%”}
{“content” => " and”}
{“content” => " L"}
{“content” => “TV”}
{“content” => " less"}
{“content” => " than"}
{“content” => " “}
{“content” => “120”}
{“content” => “%”}
{“content” => “:\n” + “\n”}
{“content” => “1”}
{“content” => “.”}
{“content” => " "}
{“content” => “Offer”}
{“content” => " "}
{“content” => “1”}
{“content” => "
\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Amount"}
{“content” => " Fin"}
{“content” => “anced”}
{“content” => “:”}
{“content” => " $“}
{“content” => “8”}
{“content” => “,”}
{“content” => “083”}
{“content” => “.”}
{“content” => “90”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Term”}
{“content” => “:”}
{“content” => " “}
{“content” => “48”}
{“content” => " months”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Rate"}
{“content” => “:”}
{“content” => " “}
{“content” => “22”}
{“content” => “.”}
{“content” => “59”}
{“content” => “%\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Monthly”}
{“content” => " Payment"}
{“content” => “:”}
{“content” => " $“}
{“content” => “257”}
{“content” => “.”}
{“content” => “29”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " DT”}
{“content” => “I”}
{“content” => “:”}
{“content” => " “}
{“content” => “5”}
{“content” => “%\n”}
{“content” => " \n”}
{“content” => “2”}
{“content” => “.”}
{“content” => " "}
{“content” => “Offer”}
{“content” => " "}
{“content” => “2”}
{“content” => "
\n"}
{“content” => " “}
{“content” => " -”}
{“content” => " Amount"}
{“content” => " Fin"}
{“content” => “anced”}
{“content” => “:”}
{“content” => " $“}
{“content” => “8”}
{“content” => “,”}
{“content” => “083”}
{“content” => “.”}
{“content” => “90”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Term”}
{“content” => “:”}
{“content” => " “}
{“content” => “60”}
{“content” => " months”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Rate"}
{“content” => “:”}
{“content” => " “}
{“content” => “22”}
{“content” => “.”}
{“content” => “84”}
{“content” => “%\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Monthly”}
{“content” => " Payment"}
{“content” => “:”}
{“content” => " $“}
{“content” => “227”}
{“content” => “.”}
{“content” => “15”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " DT”}
{“content” => “I”}
{“content” => “:”}
{“content” => " “}
{“content” => “4”}
{“content” => “%\n”}
{“content” => " \n”}
{“content” => “3”}
{“content” => “.”}
{“content” => " "}
{“content” => “Offer”}
{“content” => " "}
{“content” => “3”}
{“content” => "
\n"}
{“content” => " “}
{“content” => " -”}
{“content” => " Amount"}
{“content” => " Fin"}
{“content” => “anced”}
{“content” => “:”}
{“content” => " $“}
{“content” => “8”}
{“content” => “,”}
{“content” => “083”}
{“content” => “.”}
{“content” => “90”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Term”}
{“content” => “:”}
{“content” => " “}
{“content” => “72”}
{“content” => " months”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Rate"}
{“content” => “:”}
{“content” => " “}
{“content” => “23”}
{“content” => “.”}
{“content” => “34”}
{“content” => “%\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Monthly”}
{“content” => " Payment"}
{“content” => “:”}
{“content” => " $“}
{“content” => “209”}
{“content” => “.”}
{“content” => “60”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " DT”}
{“content” => “I”}
{“content” => “:”}
{“content” => " “}
{“content” => “4”}
{“content” => “%\n”}
{“content” => " \n”}
{“content” => “4”}
{“content” => “.”}
{“content” => " "}
{“content” => “Offer”}
{“content” => " "}
{“content” => “4”}
{“content” => "
\n"}
{“content” => " “}
{“content” => " -”}
{“content” => " Amount"}
{“content” => " Fin"}
{“content” => “anced”}
{“content” => “:”}
{“content” => " $“}
{“content” => “8”}
{“content” => “,”}
{“content” => “083”}
{“content” => “.”}
{“content” => “90”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Term”}
{“content” => “:”}
{“content” => " “}
{“content” => “84”}
{“content” => " months”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Rate"}
{“content” => “:”}
{“content” => " “}
{“content” => “23”}
{“content” => “.”}
{“content” => “59”}
{“content” => “%\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " Monthly”}
{“content” => " Payment"}
{“content” => “:”}
{“content” => " $“}
{“content” => “197”}
{“content” => “.”}
{“content” => “39”}
{“content” => “\n”}
{“content” => " “}
{“content” => " -”}
{“content” => " DT”}
{“content” => “I”}
{“content” => “:”}
{“content” => " “}
{“content” => “4”}
{“content” => “%\n” + “\n”}
{“content” => “###”}
{“content” => " Credit”}
{“content” => “worth”}
{“content” => “iness”}
{“content” => " Assessment"}
{“content” => “\n”}
{“content” => “Based”}
{“content” => " on"}
{“content” => " the"}
{“content” => " borrower"}
{“content” => “'s”}
{“content” => " credit"}
{“content” => " file"}
{“content” => “,”}
{“content” => " their"}
{“content” => " F"}
{“content” => “ICO”}
{“content” => " score"}
{“content” => " of"}
{“content” => " “}
{“content” => “607”}
{“content” => " is”}
{“content” => " higher"}
{“content” => " than"}
{“content” => " “}
{“content” => “16”}
{“content” => “%”}
{“content” => " of”}
{“content” => " consumers"}
{“content” => “.”}
{“content” => " The"}
{“content” => " borrower"}
{“content” => " has"}
{“content” => " multiple"}
{“content” => " del"}
{“content” => “inqu”}
{“content” => “encies”}
{“content” => " on"}
{“content” => " their"}
{“content” => " credit"}
{“content” => " file"}
{“content” => " and"}
{“content” => " a"}
{“content” => " public"}
{“content” => " record"}
{“content” => “,”}
{“content” => " indicating"}
{“content” => " a"}
{“content” => " history"}
{“content” => " of"}
{“content” => " difficulty"}
{“content” => " meeting"}
{“content” => " financial"}
{“content” => " obligations"}
{“content” => “.”}
{“content” => " Furthermore"}
{“content” => “,”}
{“content” => " their"}
{“content” => " revolving"}
{“content” => " balance"}
{“content” => " utilization"}
{“content” => " rate"}
{“content” => " is"}
{“content” => " quite"}
{“content” => " high"}
{“content” => " at"}
{“content” => " “}
{“content” => “58”}
{“content” => “%.\n” + “\n”}
{“content” => “However”}
{“content” => “,”}
{“content” => " the”}
{“content” => " borrower"}
{“content” => “'s”}
{“content” => " current"}
{“content” => " DT"}
{“content” => “I”}
{“content” => " is"}
{“content” => " zero"}
{“content” => “,”}
{“content” => " indicating"}
{“content” => " they"}
{“content” => " may"}
{“content” => " be"}
{“content” => " in"}
{“content” => " a"}
{“content” => " better"}
{“content” => " position"}
{“content” => " to"}
{“content” => " meet"}
{“content” => " their"}
{“content” => " monthly"}
{“content” => " loan"}
{“content” => " payments"}
{“content” => " at"}
{“content” => " present"}
{“content” => “.”}
{“content” => " Considering"}
{“content” => " the"}
{“content” => " borrower"}
{“content” => “'s”}
{“content” => " tier"}
{“content” => " “}
{“content” => “6”}
{“content” => " credit”}
{“content” => " ranking"}
{“content” => “,”}
{“content” => " our"}
{“content” => " loan"}
{“content” => " offers"}
{“content” => " come"}
{“content” => " with"}
{“content” => " higher"}
{“content” => " interest"}
{“content” => " rates"}
{“content” => " to"}
{“content” => " account"}
{“content” => " for"}
{“content” => " the"}
{“content” => " risk"}
{“content” => “.\n” + “\n”}
{“content” => “Overall”}
{“content” => “,”}
{“content” => " while"}
{“content” => " there"}
{“content” => " are"}
{“content” => " concerning"}
{“content” => " aspects"}
{“content” => " in"}
{“content” => " the"}
{“content” => " borrower"}
{“content” => “'s”}
{“content” => " credit"}
{“content” => " file"}
{“content” => “,”}
{“content” => " it"}
{“content” => " is"}
{“content” => " possible"}
{“content” => " to"}
{“content” => " extend"}
{“content” => " a"}
{“content” => " loan"}
{“content” => " under"}
{“content” => " the"}
{“content” => " provided"}
{“content” => " offers"}
{“content” => " while"}
{“content” => " ensuring"}
{“content” => " their"}
{“content” => " DT"}
{“content” => “I”}
{“content” => " remains"}
{“content” => " below"}
{“content” => " “}
{“content” => “40”}
{“content” => “%”}
{“content” => " and”}
{“content” => " the"}
{“content” => " L"}
{“content” => “TV”}
{“content” => " is"}
{“content” => " less"}
{“content” => " than"}
{“content” => " "}
{“content” => “120”}
{“content” => “%.”}
{}

Please don’t flood the screen. check Using <details> <summary> expandable content on GitHub with Markdown #details #summary #markdown #gfm #html · GitHub

@marcstein This was the intended behavior right? Or were you looking for something else?

Hey Lance,
Maybe I’m missing something, but it seems like the behavior here is to receive the full response before beginning to stream it to the output. So there is a 1-2 minute long delay before the user sees output. If you run against the openai web interface, the response is immediate, and then continues in chunks for the 1-2 minutes.

@marcstein I’ll have to look at this again! I think I need a larger prompt in my test as I missed this. Let me get back to you!

I am considering using this library in my tool, chatgpt-cli

1 Like

@marcstein Yup. I botched this. I tweaked it a bit. Please try now.

@kojix2 that is super exciting to hear!

Hi @lancecarlson , someone in our team noted that there’s a Crystal library listed here: OpenAI Platform , and I noted yours isn’t. Perhaps you want to add yours (instructions at the top)?

1 Like

@beta-ziliani Thank you for pointing this out! I just put in a submission on their request form.

1 Like