mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-22 23:16:53 +00:00
63 lines
2.2 KiB
Plaintext
63 lines
2.2 KiB
Plaintext
title: $:/plugins/tiddlywiki/ai-tools/globals
|
|
tags: $:/tags/Global
|
|
|
|
\function default-llm-completion-server()
|
|
[all[shadows+tiddlers]tag[$:/tags/AI/CompletionServer]sort[caption]first[]]
|
|
\end
|
|
|
|
<!--
|
|
Action procedure to retrieve an LLM completion, given the following parameters:
|
|
conversationTitle - Title of the tiddler containing the conversation
|
|
resultTitlePrefix - Prefix of the tiddler to be used for saving the result. If the tiddler already exists then a number will be added repeatedly until the resulting title is unique
|
|
resultTags - Tags to be applied to the result tiddler
|
|
statusTitle - Optional title of a tiddler to which the status of the request will be bound: "pending", "complete", "error"
|
|
completionServer - Optional URL of server
|
|
-->
|
|
\procedure get-llm-completion(conversationTitle,resultTitlePrefix,resultTags,statusTitle,completionServer)
|
|
<!--
|
|
Callback for the HTTP response from the LLM
|
|
-->
|
|
\procedure get-llm-completion-callback()
|
|
<%if [<status>compare:number:gteq[200]compare:number:lteq[299]] %>
|
|
<!-- Success -->
|
|
<$action-createtiddler
|
|
$basetitle=<<resultTitlePrefix>>
|
|
tags=<<resultTags>>
|
|
type="text/markdown"
|
|
role={{{ [<data>jsonget[choices],[0],[message],[role]] }}}
|
|
text={{{ [<data>jsonget[choices],[0],[message],[content]] }}}
|
|
/>
|
|
<%else%>
|
|
<!-- Error -->
|
|
<$action-createtiddler
|
|
$basetitle=<<resultTitlePrefix>>
|
|
tags=<<resultTags>>
|
|
type="text/markdown"
|
|
role="error"
|
|
text={{{ [[Error:]] [<statusText>] [<data>jsonget[error],[message]] +[join[]] }}}
|
|
/>
|
|
<%endif%>
|
|
\end get-llm-completion-callback
|
|
|
|
<$let
|
|
completionServer={{{ [<completionServer>!is[blank]else<default-llm-completion-server>] }}}
|
|
>
|
|
<$wikify name="json" text={{{ [<completionServer>get[text]] }}}>
|
|
<$action-log message="get-llm-completion"/>
|
|
<$action-log/>
|
|
<$action-sendmessage
|
|
$message="tm-http-request"
|
|
url={{{ [<completionServer>get[url]] }}}
|
|
body=<<json>>
|
|
header-content-type="application/json"
|
|
bearer-auth-token-from-store="openai-secret-key"
|
|
method="POST"
|
|
oncompletion=<<get-llm-completion-callback>>
|
|
bind-status=<<statusTitle>>
|
|
var-resultTitlePrefix=<<resultTitlePrefix>>
|
|
var-resultTags=<<resultTags>>
|
|
/>
|
|
</$wikify>
|
|
</$let>
|
|
\end get-llm-completion
|