-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdba838
commit e22d58c
Showing
6 changed files
with
315 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.PHONY: d_test d_deploy d_down d_build help | ||
|
||
IMAGE_NAME=milla | ||
|
||
d_test: | ||
nq docker compose -f ./docker-compose-devi.yaml up --build | ||
|
||
d_deploy: | ||
nq docker compose -f ./docker-compose.yaml up --build | ||
|
||
d_down: | ||
docker compose -f ./docker-compose.yaml down | ||
docker compose -f ./docker-compose-devi.yaml down | ||
|
||
d_build: d_build_distroless_vendored | ||
|
||
d_build_regular: | ||
docker build -t $(IMAGE_NAME)-f ./Dockerfile . | ||
|
||
d_build_distroless: | ||
docker build -t $(IMAGE_NAME) -f ./Dockerfile_distroless . | ||
|
||
d_build_distroless_vendored: | ||
docker build -t $(IMAGE_NAME) -f ./Dockerfile_distroless_vendored . | ||
|
||
help: | ||
@echo "d_test" | ||
@echo "d_deploy" | ||
@echo "d_down" | ||
@echo "d_build" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/alecthomas/chroma/v2/quick" | ||
"github.com/lrstanley/girc" | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
func DoORRequest( | ||
appConfig *TomlConfig, | ||
memory *[]MemoryElement, | ||
prompt string, | ||
) (string, error) { | ||
var jsonPayload []byte | ||
|
||
var err error | ||
|
||
memoryElement := MemoryElement{ | ||
Role: "user", | ||
Content: prompt, | ||
} | ||
|
||
if len(*memory) > appConfig.MemoryLimit { | ||
*memory = []MemoryElement{} | ||
|
||
for _, context := range appConfig.Context { | ||
*memory = append(*memory, MemoryElement{ | ||
Role: "assistant", | ||
Content: context, | ||
}) | ||
} | ||
} | ||
|
||
*memory = append(*memory, memoryElement) | ||
|
||
ollamaRequest := OllamaChatRequest{ | ||
Model: appConfig.Model, | ||
Messages: *memory, | ||
} | ||
|
||
jsonPayload, err = json.Marshal(ollamaRequest) | ||
if err != nil { | ||
|
||
return "", err | ||
} | ||
|
||
log.Printf("json payload: %s", string(jsonPayload)) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(appConfig.RequestTimeout)*time.Second) | ||
defer cancel() | ||
|
||
request, err := http.NewRequest(http.MethodPost, appConfig.Endpoint, bytes.NewBuffer(jsonPayload)) | ||
if err != nil { | ||
|
||
return "", err | ||
} | ||
|
||
request = request.WithContext(ctx) | ||
request.Header.Set("content-type", "application/json") | ||
request.Header.Set("Authorization", "Bearer "+appConfig.Apikey) | ||
|
||
var httpClient http.Client | ||
|
||
var dialer proxy.Dialer | ||
|
||
if appConfig.LLMProxy != "" { | ||
proxyURL, err := url.Parse(appConfig.IRCProxy) | ||
if err != nil { | ||
cancel() | ||
|
||
log.Fatal(err.Error()) | ||
} | ||
|
||
dialer, err = proxy.FromURL(proxyURL, &net.Dialer{Timeout: time.Duration(appConfig.RequestTimeout) * time.Second}) | ||
if err != nil { | ||
cancel() | ||
|
||
log.Fatal(err.Error()) | ||
} | ||
|
||
httpClient = http.Client{ | ||
Transport: &http.Transport{ | ||
Dial: dialer.Dial, | ||
}, | ||
} | ||
} | ||
response, err := httpClient.Do(request) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
log.Println("response body:", response.Body) | ||
|
||
var orresponse ORResponse | ||
|
||
err = json.NewDecoder(response.Body).Decode(&orresponse) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var result string | ||
|
||
for _, choice := range orresponse.Choices { | ||
result += choice.Message.Content + "\n" | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func ORRequestProcessor( | ||
appConfig *TomlConfig, | ||
client *girc.Client, | ||
event girc.Event, | ||
memory *[]MemoryElement, | ||
prompt string, | ||
) string { | ||
response, err := DoORRequest(appConfig, memory, prompt) | ||
if err != nil { | ||
client.Cmd.ReplyTo(event, "error: "+err.Error()) | ||
|
||
return "" | ||
} | ||
|
||
assistantElement := MemoryElement{ | ||
Role: "assistant", | ||
Content: response, | ||
} | ||
|
||
*memory = append(*memory, assistantElement) | ||
|
||
log.Println(response) | ||
|
||
var writer bytes.Buffer | ||
|
||
err = quick.Highlight(&writer, | ||
response, | ||
"markdown", | ||
appConfig.ChromaFormatter, | ||
appConfig.ChromaStyle) | ||
if err != nil { | ||
client.Cmd.ReplyTo(event, "error: "+err.Error()) | ||
|
||
return "" | ||
} | ||
|
||
return writer.String() | ||
} | ||
|
||
func ORHandler( | ||
irc *girc.Client, | ||
appConfig *TomlConfig, | ||
memory *[]MemoryElement) { | ||
irc.Handlers.AddBg(girc.PRIVMSG, func(client *girc.Client, event girc.Event) { | ||
if !strings.HasPrefix(event.Last(), appConfig.IrcNick+": ") { | ||
return | ||
} | ||
|
||
if appConfig.AdminOnly { | ||
byAdmin := false | ||
|
||
for _, admin := range appConfig.Admins { | ||
if event.Source.Name == admin { | ||
byAdmin = true | ||
} | ||
} | ||
|
||
if !byAdmin { | ||
return | ||
} | ||
} | ||
|
||
prompt := strings.TrimPrefix(event.Last(), appConfig.IrcNick+": ") | ||
log.Println(prompt) | ||
|
||
if string(prompt[0]) == "/" { | ||
runCommand(client, event, appConfig) | ||
|
||
return | ||
} | ||
|
||
result := ORRequestProcessor(appConfig, client, event, memory, prompt) | ||
if result != "" { | ||
sendToIRC(client, event, result, appConfig.ChromaFormatter) | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.