Skip to content

Commit f12f6c6

Browse files
authored
Merge pull request #204 from hchen2020/master
IConversationAttachmentService
2 parents 2f2ef32 + b55abdf commit f12f6c6

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ It's written in C# running on .Net Core that is full cross-platform framework, t
2424
* Built-in multi-agents and conversation with state management.
2525
* Support multiple LLM Planning approaches to handle different tasks.
2626
* Built-in RAG related interfaces, Memeory based vector searching.
27-
* Support multiple LLM platforms (ChatGPT 3.5 / 4.0, PaLM 2, LLaMA 2).
27+
* Support multiple LLM platforms (ChatGPT 3.5 / 4.0, PaLM 2, LLaMA 2, HuggingFace).
2828
* Allow multiple agents with different responsibilities cooperate to complete complex tasks.
2929
* Build, test, evaluate and audit your LLM agent in one place.
3030
* Support different open source UI [Chatbot UI](src/Plugins/BotSharp.Plugin.ChatbotUI/Chatbot-UI.md), [HuggingChat UI](src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat-UI.md).
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Conversations;
2+
3+
public interface IConversationAttachmentService
4+
{
5+
string GetDirectory(string conversationId);
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace BotSharp.Abstraction.Conversations.Models;
2+
3+
public class Attachment
4+
{
5+
public string ContentType { get; set; }
6+
7+
//
8+
// Summary:
9+
// Gets the raw Content-Disposition header of the uploaded file.
10+
public string ContentDisposition { get; set; }
11+
12+
//
13+
// Summary:
14+
// Gets the file length in bytes.
15+
public long Length { get; set; }
16+
17+
//
18+
// Summary:
19+
// Gets the file name from the Content-Disposition header.
20+
public string FileName { get; set; }
21+
}

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
102102
services.AddScoped<IEvaluatingService, EvaluatingService>();
103103
services.AddScoped<IExecutionLogger, ExecutionLogger>();
104104

105+
services.AddScoped<IConversationAttachmentService, ConversationAttachmentService>();
106+
105107
return services;
106108
}
107109

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using BotSharp.Abstraction.Repositories;
2+
using System.IO;
3+
4+
namespace BotSharp.Core.Conversations.Services;
5+
6+
public class ConversationAttachmentService : IConversationAttachmentService
7+
{
8+
private readonly BotSharpDatabaseSettings _dbSettings;
9+
private readonly IServiceProvider _services;
10+
11+
public ConversationAttachmentService(
12+
BotSharpDatabaseSettings dbSettings,
13+
IServiceProvider services)
14+
{
15+
_dbSettings = dbSettings;
16+
_services = services;
17+
}
18+
19+
public string GetDirectory(string conversationId)
20+
{
21+
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId, "attachments");
22+
if (!Directory.Exists(dir))
23+
{
24+
Directory.CreateDirectory(dir);
25+
}
26+
return dir;
27+
}
28+
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using BotSharp.Abstraction.Conversations.Models;
33
using BotSharp.Abstraction.Models;
44
using BotSharp.OpenAPI.ViewModels.Conversations;
5+
using Microsoft.AspNetCore.Http;
6+
using System.Net.Http.Headers;
57

68
namespace BotSharp.OpenAPI.Controllers;
79

@@ -78,4 +80,31 @@ await conv.SendMessage(agentId, inputMsg,
7880

7981
return response;
8082
}
83+
84+
[HttpPost("/conversation/{agentId}/{conversationId}/attachments")]
85+
public IActionResult UploadAttachments([FromRoute] string agentId,
86+
[FromRoute] string conversationId,
87+
IFormFile[] files)
88+
{
89+
if (files != null && files.Length > 0)
90+
{
91+
var attachmentService = _services.GetRequiredService<IConversationAttachmentService>();
92+
var dir = attachmentService.GetDirectory(conversationId);
93+
foreach (var file in files)
94+
{
95+
// Save the file, process it, etc.
96+
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
97+
var filePath = Path.Combine(dir, fileName);
98+
99+
using (var stream = new FileStream(filePath, FileMode.Create))
100+
{
101+
file.CopyTo(stream);
102+
}
103+
}
104+
105+
return Ok(new { message = "File uploaded successfully." });
106+
}
107+
108+
return BadRequest(new { message = "Invalid file." });
109+
}
81110
}

0 commit comments

Comments
 (0)