-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai-request.rkt
76 lines (63 loc) · 2.38 KB
/
openai-request.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#lang racket/base
(require json
net/url-string
net/http-client
racket/port
racket/match)
(provide openai-url-convert
send-openai-request
get-openai-reply)
(define (port->string/utf-8 port)
(bytes->string/utf-8 (port->bytes port)))
(define (openai-url-convert openai-base-url)
(define openai-base (url-host openai-base-url))
(define openai-uri
(url->string
(struct-copy url openai-base-url
[scheme #f]
[host #f]
[path-absolute? #t]
[path (append
(url-path openai-base-url)
(list
(path/param "chat" '())
(path/param "completions" '())))])))
(values openai-base openai-uri))
(define (send-openai-request model messages
#:openai-base openai-base
#:openai-uri openai-uri
#:api-key api-key
#:temperature [temperature 0.0])
(define data
(jsexpr->bytes
`#hash([model . ,model]
[temperature . ,temperature]
[messages . ,messages])))
(define-values (status response body)
(http-sendrecv openai-base openai-uri
#:data data
#:method "POST"
#:ssl? 'tls12
#:headers `("Content-Type: application/json"
,(format "Authorization: Bearer ~a" api-key))))
(define js-result
(string->jsexpr (port->string/utf-8 body)))
js-result)
(define (get-openai-reply model message
#:openai-base openai-base
#:openai-uri openai-uri
#:api-key api-key
#:temperature [temperature 0.0])
(define messages (list `#hash([role . "user"]
[content . ,message])))
(define resp (send-openai-request model messages
#:openai-base openai-base
#:openai-uri openai-uri
#:api-key api-key
#:temperature temperature))
(match resp
[(hash* ['choices
(cons (hash* ['message (hash* ['content content])])
_)])
content]
[_ #f]))