1
+ package sseread
2
+
3
+ import (
4
+ "bytes"
5
+ "encoding/json"
6
+ "errors"
7
+ "io"
8
+ "net/http"
9
+ )
10
+
11
+ // https://developers.cloudflare.com/workers-ai/models/zephyr-7b-beta-awq/#using-streaming
12
+ type CfTextGenerationResponse struct {
13
+ Response string `json:"response"`
14
+ P string `json:"p"`
15
+ }
16
+
17
+ type CfTextGenerationMsg struct {
18
+ Role string `json:"role"`
19
+ Content string `json:"content"`
20
+ }
21
+
22
+
23
+ type CfTextGenerationArg struct {
24
+ Stream bool `json:"stream,omitempty"`
25
+ Messages []CfTextGenerationMsg `json:"messages,omitempty"`
26
+ }
27
+
28
+ func (c * CfTextGenerationArg ) body () (io.ReadCloser , error ) {
29
+ buff := bytes .NewBuffer (nil )
30
+ err := json .NewEncoder (buff ).Encode (c )
31
+ return io .NopCloser (buff ), err
32
+ }
33
+
34
+
35
+ type CloudflareAI struct {
36
+ AccountID string
37
+ APIToken string
38
+
39
+ }
40
+
41
+ var httpClient = & http.Client {}
42
+
43
+
44
+
45
+ var modelsTextGeneration = []string {
46
+ //https://dash.cloudflare.com/0a76b889e644c012524110042e6f197e/ai/workers-ai
47
+ //page 1
48
+ "@cf/meta/llama-2-7b-chat-fp16" ,
49
+ "@cf/mistral/mistral-7b-instruct-v0.1" ,
50
+ "@cf/meta/llama-2-7b-chat-int8" ,
51
+ "@cf/qwen/qwen1.5-0.5b-chat" ,
52
+ "@hf/thebloke/llamaguard-7b-awq" ,
53
+ "@hf/thebloke/neural-chat-7b-v3-1-awq" ,
54
+ "@cf/deepseek-ai/deepseek-math-7b-base" ,
55
+ "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" ,
56
+ "@hf/thebloke/orca-2-13b-awq" ,
57
+ "@hf/thebloke/codellama-7b-instruct-awq" ,
58
+ //page 2
59
+ "@cf/thebloke/discolm-german-7b-v1-awq" ,
60
+ "@hf/thebloke/mistral-7b-instruct-v0.1-awq" ,
61
+ "@hf/thebloke/openchat_3.5-awq" ,
62
+ "@cf/qwen/qwen1.5-7b-chat-awq" ,
63
+ "@hf/thebloke/llama-2-13b-chat-awq" ,
64
+ "@hf/thebloke/deepseek-coder-6.7b-base-awq" ,
65
+ "@hf/thebloke/openhermes-2.5-mistral-7b-awq" ,
66
+ "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" ,
67
+ "@cf/deepseek-ai/deepseek-math-7b-instruct" ,
68
+ "@cf/tiiuae/falcon-7b-instruct" ,
69
+ //page 3
70
+ "@hf/thebloke/zephyr-7b-beta-awq" ,
71
+ "@cf/qwen/qwen1.5-1.8b-chat" ,
72
+ "@cf/defog/sqlcoder-7b-2" ,
73
+ "@cf/microsoft/phi-2" ,
74
+ "@cf/qwen/qwen1.5-14b-chat-awq" ,
75
+ "@cf/openchat/openchat-3.5-0106" ,
76
+ }
77
+
78
+ func (c * CloudflareAI ) modelCheck (model string ) error {
79
+ for _ , v := range modelsTextGeneration {
80
+ if v == model {
81
+ return nil
82
+ }
83
+ }
84
+ return errors .New ("model not found: " + model )
85
+ }
86
+
87
+ func (c * CloudflareAI ) Do (model string , arg * CfTextGenerationArg ) (* http.Response , error ) {
88
+ if c .AccountID == "" || c .APIToken == "" {
89
+ return nil , errors .New ("CF_ACCOUNT_ID and CF_API_TOKEN environment variables are required" )
90
+ }
91
+
92
+ if err := c .modelCheck (model ); err != nil {
93
+ return nil , err
94
+ }
95
+
96
+ body , err := arg .body ()
97
+ if err != nil {
98
+ return nil , err
99
+ }
100
+
101
+ req , err := http .NewRequest ("POST" , "https://api.cloudflare.com/client/v4/accounts/" + c .AccountID + "/ai/run/" + model , body )
102
+ if err != nil {
103
+ return nil , err
104
+ }
105
+ req .Header .Set ("Authorization" , "Bearer " + c .APIToken )
106
+ return httpClient .Do (req )
107
+ }
0 commit comments