Advertisement
Zektrix1

Local Ollama API (More Modular) For AHK v2

Sep 15th, 2024 (edited)
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. /*
  2. Local Ollama API (More Modular) For AHK v2
  3. Made by Zektrix
  4. Z3ktrix
  5.  
  6. Each seperate call can be summed up by changing the variables in any combination.
  7. Using this:
  8. ```AHKv2
  9. Response := Ollama_Call(URL,Model,System,Prompt)
  10. ```
  11. Using Variable 'Response' to display or capture it how you like.
  12. Possibly sending it right back into another call!
  13. */
  14.  
  15. #Requires AutoHotkey v2.0
  16. #SingleInstance Force
  17.  
  18. Numpad9::Reload
  19.  
  20. Numpad7::
  21. {
  22. URL := "
  23. (
  24. http://localhost:11434/api/generate
  25. )"
  26.  
  27. Model := "
  28. (
  29. dolphin-mistral
  30. )"
  31.  
  32. System := "
  33. (
  34. You are a balloon animal sculter trying desperately to get some work.
  35. Don't give up!
  36. Convince them to give you work in a kind of pathetic, sad, and over all desperate times way.
  37. )"
  38.  
  39. Prompt := "
  40. (
  41. Hello, there!"
  42. )"
  43.  
  44. Response := Ollama_Call(URL,Model,System,Prompt)
  45.  
  46. A_ClipBoard := Response
  47. MsgBox Response
  48. }
  49.  
  50. Ollama_Call(URL,Model,System,Prompt)
  51. {
  52. Cleaned_Variable_For_JSON := Model
  53. Model := Clean_Variable_For_JSON(Cleaned_Variable_For_JSON)
  54. Cleaned_Variable_For_JSON := System
  55. System := Clean_Variable_For_JSON(Cleaned_Variable_For_JSON)
  56. Cleaned_Variable_For_JSON := Prompt
  57. Prompt := Clean_Variable_For_JSON(Cleaned_Variable_For_JSON)
  58. Payload := "
  59. (
  60. {
  61. "System": "System_PlaceHolder",
  62. "model": "Model_PlaceHolder",
  63. "prompt": "Prompt_PlaceHolder",
  64. "stream": false
  65. }
  66. )"
  67. Payload := StrReplace(Payload, "Model_PlaceHolder", Model)
  68. Payload := StrReplace(Payload, "System_PlaceHolder", System)
  69. Payload := StrReplace(Payload, "Prompt_PlaceHolder", Prompt)
  70. WinHttp := ComObject("WinHttp.WinHttpRequest.5.1")
  71. WinHttp.Open("POST", URL, True)
  72. WinHttp.SetRequestHeader("Content-Type", "application/json")
  73. WinHttp.Send(Payload)
  74. WinHttp.WaitForResponse
  75. Response := WinHttp.ResponseText
  76. Try
  77. RegExMatch(Response, "s)(?<=`",`"response`":`").*?(?=`",`"done`":true)", &Match)
  78. Try
  79. Response := Match[0]
  80. Response := StrReplace(Response, "\`"", "`"")
  81. Response := StrReplace(Response, "\n", "`n")
  82. Return Response
  83. }
  84.  
  85. Clean_Variable_For_JSON(Cleaned_Variable_For_JSON)
  86. {
  87. Cleaned_Variable_For_JSON := StrReplace(Cleaned_Variable_For_JSON, "\", "\\")
  88. Cleaned_Variable_For_JSON := StrReplace(Cleaned_Variable_For_JSON, "`r`n", "\n")
  89. Cleaned_Variable_For_JSON := StrReplace(Cleaned_Variable_For_JSON, "`r", "\r")
  90. Cleaned_Variable_For_JSON := StrReplace(Cleaned_Variable_For_JSON, "`n", "\n")
  91. Cleaned_Variable_For_JSON := StrReplace(Cleaned_Variable_For_JSON, A_Tab, "\t")
  92. Cleaned_Variable_For_JSON := StrReplace(Cleaned_Variable_For_JSON, "`"", "\`"")
  93. Return Cleaned_Variable_For_JSON
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement