Skip to content

Commit 9aa68ca

Browse files
authored
Create ai-request-llama-llm.pq
1 parent 86ed7db commit 9aa68ca

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

source/web/ai-request-llama-llm.pq

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
let
2+
// I updated the code from 5:57 at <https://www.youtube.com/watch?v=L3xfWiMSMiA&t=357s>
3+
4+
base_url = "http://localhost:11434",
5+
prompt = "do llama magic",
6+
DoubleQuote = """", // or: "#(0022)"
7+
EscapedDoubleQuote = "\""", // or: "\#(0022)"
8+
9+
//
10+
Text.EscapeQuote = (string as text) =>
11+
Text.Replace( string, DoubleQuote, EscapedDoubleQuote ),
12+
13+
// sugar to call the API
14+
// I updated the original requestBody to use standard power query
15+
// that saves you having to manually write json and adding quoted strings
16+
Llama.Request = ( prompt as text ) =>
17+
let
18+
json = [
19+
model = "llama3.2:latest",
20+
// escaping might be redundant because of Json.FromValue
21+
// I left it in so you could test it
22+
prompt = Text.EscapeQuote( prompt ),
23+
stream = false
24+
],
25+
return = Web.Contents(
26+
base_url,
27+
[
28+
// this encodes to bytes using Text.Encoding.Utf8 by default
29+
Content = Json.FromValue( json ),
30+
RelativePath = "api/generate",
31+
Headers = [
32+
#"Content-Type" = "application/json"
33+
],
34+
ManualStatusHandling = {200..299}
35+
]
36+
)
37+
in
38+
return,
39+
40+
// inspect the Status code, headers, requestUrl, etc....
41+
Web.InspectResponse = (response as binary) as record => [
42+
Bytes = response,
43+
HadError = StatusCode <> 200,
44+
StatusCode = Meta[Response.Status],
45+
ContentType = Meta[Content.Type],
46+
RequestUrl = Meta[Content.Uri](),
47+
Meta = Value.Metadata( Bytes ),
48+
AsJson =
49+
try Json.Document( Bytes, TextEncoding.Utf8 )
50+
catch (e) => null,
51+
52+
RawText = Text.FromBinary( Bytes, TextEncoding.Utf8 )
53+
],
54+
55+
Test = [
56+
response = Llama.Request( prompt ),
57+
Info = Web.InspectResponse( response ),
58+
59+
// for a comparison, here's a public JSON response, no auth needed
60+
Info2 = Web.InspectResponse( Web.Contents( "https://httpbin.org/json" ) ),
61+
62+
RawInfo = Value.Metadata( response ) // here's the base data that Web.InspectResponse uses
63+
]
64+
in
65+
Test

0 commit comments

Comments
 (0)