Skip to content

Commit

Permalink
fix vision
Browse files Browse the repository at this point in the history
  • Loading branch information
boogiedk committed Dec 6, 2023
1 parent 7035298 commit 34c9e27
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
9 changes: 5 additions & 4 deletions SerfBot/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ let commandHandler command =
try
match command with
| Ping -> "pong"
| Vision (imageLink, userText) ->
descriptionAnalyzedImage imageLink
| Vision (userText, base64Img) ->
descriptionAnalyzedImage userText base64Img
|> Async.RunSynchronously
| Context userText ->
setupContext userText
Expand All @@ -19,8 +19,9 @@ let commandHandler command =
gptAnswer userText
|> Async.RunSynchronously
| Weather city ->
let weather = WeatherApi.getWeatherAsync city |> Async.RunSynchronously
let weather = WeatherApi.getWeatherAsync city
|> Async.RunSynchronously
$"Погода в %s{city}: %s{weather}"
| _ -> "Некорректная команда для GPT"
| _ -> "Некорректная команда"
with
| ex -> sprintf "Ошибка: %s" ex.Message
8 changes: 4 additions & 4 deletions SerfBot/OpenAiApi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ let gptAnswer userQuestion =
return ex.Message
}

let descriptionAnalyzedImage imageLink =
let descriptionAnalyzedImage userText base64Img =
async {
try
let api = OpenAIClient(Configuration.config.OpenAiApiToken)

let userText2 = if userText == null then "Что на фото?" else userText
let messages =
[
Message(Role.System, Option.get currentContext)
Message(Role.User,
[
Content(ContentType.Text, "Что на этой картинке?")
Content(ContentType.ImageUrl, imageLink)
Content(ContentType.Text, userText2)
Content(ContentType.ImageUrl, $"data:image/jpeg;base64,{base64Img}")
])
]

Expand Down
55 changes: 34 additions & 21 deletions SerfBot/TelegramBot.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module SerfBot.TelegramBot

open System
open System.IO
open System.Net.Http
open ExtCore.Control.Collections
open Funogram.Api
open Funogram.Telegram
open Funogram.Telegram.Bot
open Funogram.Telegram.Types
open SerfBot.Log
Expand All @@ -25,39 +28,49 @@ let processCommand (ctx: UpdateContext, command: MessageReplayCommand) =
|> Async.RunSynchronously
|> ignore

// obsolet
//let updateArrived (ctx: UpdateContext) =
// match ctx.Update.Message with
// | Some { MessageId = messageId; Chat = chat; Text = text; Photo = photo; Voice = voice } ->
// let user = ctx.Update.Message.Value.From.Value
// match isValidUser user.Id with
// | Some () ->
// logInfo $"Message from user {Option.get user.Username} received: {Option.get text}"
// let command, userMessage = extractCommand text.Value
// match Commands.commandHandlers.TryGetValue command with
// | true, handler ->
// let replyText = handler userMessage
// processCommand(ctx, { Chat = chat; MessageId = messageId; Text = text; ReplayText = replyText; })
// | _ -> ()
// | None ->
// sprintf "Authorize error." |> logInfo
// | _ -> ()
let streamToBase64 (stream: Stream) =
use ms = new MemoryStream()
stream.CopyTo(ms)
let buffer = ms.ToArray()
Convert.ToBase64String(buffer)

let extractFileDataAsBase64 (fileResult: Result<File,Funogram.Types.ApiResponseError>) =
match fileResult with
| Ok(file) ->
let filePath = Option.get file.FilePath // предполагается, что у вас есть свойство "Path" в типе "File"
let apiUrl = $"https://api.telegram.org/file/bot{Configuration.config.TelegramBotToken}/{filePath}"
use httpStream = new HttpClient()
let f = httpStream.GetStreamAsync(apiUrl) |> Async.AwaitTask |> Async.RunSynchronously
let base64 = streamToBase64 f
base64



let handleFiles fileId ctx =
let file = Req.GetFile.Make fileId
|> api ctx.Config
|> Async.RunSynchronously
let base64Img = extractFileDataAsBase64 file
base64Img


let updateArrivedMessage (ctx: UpdateContext) =
match ctx.Update.Message with
| Some { MessageId = messageId; Chat = chat; Text = text; Photo = photo } ->
| Some { MessageId = messageId; Chat = chat; Text = text; Photo = photo; Caption = caption } ->
let user = ctx.Update.Message.Value.From.Value

let base64Img = if photo.IsSome then handleFiles photo.Value[0].FileId ctx else ""
let message = if text.IsSome then text.Value elif caption.IsSome then caption.Value else ""
match isValidUser user.Id with
| Some () ->
logInfo $"Message from user {Option.get user.Username} received: {Option.get text}"
let command, userMessage = extractCommand text.Value
logInfo $"Message from user {Option.get user.Username} received: " //{Option.get text}
let command, userMessage = extractCommand message
let commandType =
match command with
| "!ping" -> Ping
| "погода" -> Weather userMessage
| "!context" -> Context userMessage
| "!vision" -> Vision (userMessage, "test")
| "!vision" -> Vision (userMessage, base64Img)
| "гпт" -> Question userMessage
| _ -> Other userMessage

Expand Down
7 changes: 0 additions & 7 deletions SerfBot/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ type ApplicationConfiguration = {
UserIds: int64[]
}

type CommandHandler = string -> string

type Commands = {
Name: string
Handler: CommandHandler
}

type Command =
| Ping
| Question of string
Expand Down

0 comments on commit 34c9e27

Please sign in to comment.