-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcode_assistant.cpp.deprecated
165 lines (165 loc) · 5.46 KB
/
code_assistant.cpp.deprecated
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// #include <co_async/co_async.hpp>
// #include <co_async/std.hpp>
//
// using namespace co_async;
// using namespace std::literals;
//
// struct ChatCompletionRequest {
// struct Message {
// std::string role;
// std::string content;
//
// REFLECT(role, content);
// };
//
// std::vector<Message> messages;
// std::string model;
// std::optional<double> frequency_penalty{};
// std::optional<int> max_tokens{};
// std::optional<double> presence_penalty{};
// std::optional<std::vector<std::string>> stop{};
// std::optional<double> temperature{};
// std::optional<double> top_p{};
// std::optional<bool> logprobs{};
// std::optional<int> top_logprobs{};
// std::optional<bool> stream{};
//
// REFLECT(messages, model, frequency_penalty, max_tokens, presence_penalty, stop, temperature, top_p, logprobs, top_logprobs, stream);
// };
//
// struct ChatCompletionResult {
// struct Choice {
// struct Message {
// std::optional<std::string> role;
// std::optional<std::string> content;
//
// REFLECT(role, content);
// };
//
// struct Logprobs {
// struct Content {
// struct Logprob {
// std::string token;
// double logprob;
// std::vector<std::uint8_t> bytes;
//
// REFLECT(token, logprob, bytes);
// };
//
// std::string token;
// double logprob;
// std::vector<std::uint8_t> bytes;
// std::vector<Logprob> top_logprobs;
//
// REFLECT(token, logprob, bytes, top_logprobs);
// };
//
// std::vector<Content> content;
//
// REFLECT(content);
// };
//
// int index;
// std::optional<Message> delta;
// std::optional<Message> message;
// std::optional<std::string> finish_reason;
// std::optional<Logprobs> logprobs;
//
// REFLECT(index, delta, finish_reason, logprobs);
// };
//
// struct Usage {
// int completion_tokens;
// int prompt_tokens;
// int total_tokens;
//
// REFLECT(completion_tokens, prompt_tokens, total_tokens);
// };
//
// std::string id;
// std::vector<Choice> choices;
// std::int64_t created;
// std::string model;
// std::optional<Usage> usage;
//
// REFLECT(id, choices, created, model, usage);
// };
//
// static std::string form_code_complete(std::string_view code) {
// std::string res;
// res.append("Edit the following code to make it complete. Output the completed code without any additional text. Do not explain.\n");
// /* res.append("```"); */
// /* res.append(language); */
// /* res.append("\n"); */
// res.append(code);
// /* res.append("\n"); */
// /* res.append("```"); */
// return res;
// }
//
// inline HTTPConnectionPool pool;
//
// static Task<Expected<OwningStream>> evaluate(std::string prompt) {
// std::string authorization;
// if (auto p = std::getenv("OPENAI_API_KEY")) {
// authorization.append("Bearer ");
// authorization.append(p);
// }
// auto conn = co_await co_await pool.connect("https://api.openai.com");
// HTTPRequest req = {
// .method = "POST",
// .uri = URI::parse("/v1/chat/completions"),
// .headers = {
// {"content-type", "application/json"},
// {"authorization", String{authorization}},
// },
// };
// auto compReq = ChatCompletionRequest{
// .messages = {
// {.role = "user", .content = prompt},
// },
// .model = "gpt-3.5-turbo",
// .max_tokens = 64,
// .stream = true,
// };
// auto [res, body] = co_await co_await conn->request_streamed(req, json_encode(compReq));
// auto [r, w] = pipe_stream();
// co_spawn(pipe_bind(std::move(w), [conn = std::move(conn), body = std::move(body)] (OwningStream &w) mutable -> Task<Expected<>> {
// while (auto tmp = co_await body.getline('\n')) {
// std::string_view line = *tmp;
// if (line.starts_with("data: "sv)) {
// line.remove_prefix(6);
// if (line == "[DONE]"sv) {
// break;
// }
// auto compRes = co_await json_decode<ChatCompletionResult>(line);
// auto delta = compRes.choices.at(0).delta.value().content.value_or("");
// co_await co_await w.putchunk(delta);
// }
// }
// co_await co_await body.dropall();
// co_return {};
// }));
// co_return std::move(r);
// }
//
// static void code_complete(std::string code, std::function<void(std::string)> callback) {
// IOContext().join(co_bind([code = std::move(code), callback = std::move(callback)] () -> Task<Expected<>> {
// auto prompt = form_code_complete(code);
// auto stream = co_await co_await evaluate(std::move(prompt));
// while (auto chunk = co_await stream.getchunk()) {
// if (!chunk->empty()) {
// callback(std::string{std::move(*chunk)});
// }
// }
// co_return {};
// })).value();
// }
//
// int main() {
// auto code = "#include <studio>\nint mian() {\nhow to print(hello world) here? help!\n}"s;
// code_complete(code, [] (std::string chunk) {
// std::cerr << chunk;
// });
// return 0;
// }