-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_client.mojo
29 lines (20 loc) · 873 Bytes
/
api_client.mojo
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
from python import Python
fn call(prompt: String) raises -> String:
let HOST = '127.0.0.1'
let PORT = 65432
let py_builtins = Python.import_module("builtins")
let socket = Python.import_module("socket")
let altered_prompt = "<|im_start|>user\n" + prompt + "<|im_end|>\n<|im_start|>assistant\n"
let s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_ = s.connect((HOST, PORT))
_ = s.sendall(py_builtins.bytes(altered_prompt, "utf-8"))
# Convert to Mojo String and remove b''
var response = s.recv(1024).to_string()[2:-1]
_ = s.close()
# Add newlines and remove initial prompt
response = response.replace("\\n", "\n")[len(altered_prompt):]
return response
fn main() raises:
let prompt = "Give me a python function to generate Fibonacci sequence"
let response = call(prompt)
print(response)