Skip to content

Commit

Permalink
Merge pull request #305 from SciSharp/mergecode
Browse files Browse the repository at this point in the history
merge code
  • Loading branch information
geffzhang authored Feb 18, 2024
2 parents 14f2570 + c67dad4 commit f342498
Show file tree
Hide file tree
Showing 77 changed files with 1,148 additions and 290 deletions.
26 changes: 13 additions & 13 deletions docs/quick-start/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Building solution using dotnet CLI (preferred).

### Clone the source code and build
```powershell
PS D:\> git clone https://github.com/Oceania2018/BotSharp
PS D:\> git clone https://github.com/SciSharp/BotSharp
PS D:\> cd BotSharp
PS D:\> dotnet build
```
Expand Down Expand Up @@ -62,28 +62,28 @@ So far, you have set up the Bot's running and development environment, but you c

**Ignore below section if you're going to just use REST API to interact with your bot.**

### Launch a chatbot UI (Optional)
You can use a third-party open source UI for debugging and development, or you can directly use the REST API to integrate with your system.
If you want to use the [Chatbot UI](https://github.com/mckaywrigley/chatbot-ui) as a front end.
### Launch a BotSharp UI (Optional)
BotSharp has an official front-end project to be used in conjunction with the backend. The main function of this project is to allow developers to visualize various configurations of the backend.
```powershell
PS D:\> git clone https://github.com/mckaywrigley/chatbot-ui
PS D:\> cd chatbot-ui
PS D:\> cd npm i
PS D:\> cd npm run dev
PS D:\> git clone https://github.com/SciSharp/BotSharp-UI
PS D:\> cd BotSharp-UI
PS D:\> npm install
PS D:\> npm run dev
```

Update API url in `.env.local` to your localhost BotSharp backend service.
Update API url in `.env` to your localhost BotSharp backend service.
```config
OPENAI_API_HOST=http://localhost:5500
PUBLIC_SERVICE_URL=http://localhost:5500
PUBLIC_LIVECHAT_HOST=http://localhost:5015
```

Point your web browser at http://localhost:3000 and enjoy Chatbot with BotSharp.
Point your web browser at http://localhost:5015 and enjoy Chatbot with BotSharp.

![alt text](assets/ChatbotUIHome.png "Title")
![BotSharp UI Router](assets/BotSharp-UI-Router.png)

## Install in NuGet
If you don't want to use the source code to experience this framework, you can also directly install the [NuGet packages](https://www.nuget.org/packages?q=BotSharp) released by BotSharp, and install different function packages according to the needs of your project. Before installing, please read the documentation carefully to understand the functions that different modules can provide.

```powershell
PS D:\> Install-Package BotSharp.Core
```
```
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;

namespace BotSharp.Abstraction.Conversations;
Expand All @@ -14,6 +15,8 @@ public interface IConversationService
Task<List<Conversation>> GetLastConversations();
Task<bool> DeleteConversation(string id);
Task<bool> TruncateConversation(string conversationId, string messageId);
Task<List<ConversationContentLogModel>> GetConversationContentLogs(string conversationId);
Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId);

/// <summary>
/// Send message to LLM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public class Conversation
public string Id { get; set; } = string.Empty;
public string AgentId { get; set; } = string.Empty;
public string UserId { get; set; } = string.Empty;

/// <summary>
/// Agent task id
/// </summary>
public string? TaskId { get; set; }
public string Title { get; set; } = string.Empty;

[JsonIgnore]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Conversations.Models;

public class ConversationSenderActionModel
{
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; }
[JsonPropertyName("sender_action")]
public SenderActionEnum SenderAction { get; set; }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class ConversationSetting
public int MaxRecursiveDepth { get; set; } = 3;
public bool EnableLlmCompletionLog { get; set; }
public bool EnableExecutionLog { get; set; }
public bool EnableContentLog { get; set; }
public bool EnableStateLog { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
namespace BotSharp.Abstraction.Loggers.Models;

public class StreamingLogModel
public class ConversationContentLogModel
{
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; }
[JsonPropertyName("message_id")]
public string MessageId { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("role")]
public string Role { get; set; }

[JsonPropertyName("content")]
public string Content { get; set; }

[JsonPropertyName("created_at")]
public DateTime CreateTime { get; set; }
public DateTime CreateTime { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BotSharp.Abstraction.Loggers.Models;

public class ConversationStateLogModel
{
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; }
[JsonPropertyName("message_id")]
public string MessageId { get; set; }
[JsonPropertyName("states")]
public Dictionary<string, string> States { get; set; }
[JsonPropertyName("created_at")]
public DateTime CreateTime { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Abstraction.Loggers.Models;

public class LlmCompletionLog
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
using System.Runtime.Serialization;

namespace BotSharp.Abstraction.Messaging.Enums;

public enum SenderActionEnum
{
[EnumMember(Value = "typing_on")]
TypingOn,
TypingOn = 1,
[EnumMember(Value = "typing_off")]
TypingOff,
[EnumMember(Value = "mark_seen")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public class MessageConfig : TruncateMessageRequest
/// Conversation states from input
/// </summary>
public List<string> States { get; set; } = new List<string>();

/// <summary>
/// Agent task id
/// </summary>
public string? TaskId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public class ConversationFilter
public string? Status { get; set; }
public string? Channel { get; set; }
public string? UserId { get; set; }

/// <summary>
/// Agent task id
/// </summary>
public string? TaskId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
Expand Down Expand Up @@ -70,6 +71,16 @@ public interface IBotSharpRepository
void SaveLlmCompletionLog(LlmCompletionLog log);
#endregion

#region Conversation Content Log
void SaveConversationContentLog(ConversationContentLogModel log);
List<ConversationContentLogModel> GetConversationContentLogs(string conversationId);
#endregion

#region Conversation State Log
void SaveConversationStateLog(ConversationStateLogModel log);
List<ConversationStateLogModel> GetConversationStateLogs(string conversationId);
#endregion

#region Statistics
void IncrementConversationCount();
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories;

namespace BotSharp.Core.Conversations.Services;

public partial class ConversationService
{
public async Task<List<ConversationContentLogModel>> GetConversationContentLogs(string conversationId)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var logs = db.GetConversationContentLogs(conversationId);
return await Task.FromResult(logs);
}


public async Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var logs = db.GetConversationStateLogs(conversationId);
return await Task.FromResult(logs);
}
}
25 changes: 25 additions & 0 deletions src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Filters;
Expand Down Expand Up @@ -255,6 +256,30 @@ public void SaveLlmCompletionLog(LlmCompletionLog log)
}
#endregion

#region Conversation Content Log
public void SaveConversationContentLog(ConversationContentLogModel log)
{
throw new NotImplementedException();
}

public List<ConversationContentLogModel> GetConversationContentLogs(string conversationId)
{
throw new NotImplementedException();
}
#endregion

#region Conversation State Log
public void SaveConversationStateLog(ConversationStateLogModel log)
{
throw new NotImplementedException();
}

public List<ConversationStateLogModel> GetConversationStateLogs(string conversationId)
{
throw new NotImplementedException();
}
#endregion

#region Stats
public void IncrementConversationCount()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
var records = new List<Conversation>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
var pager = filter?.Pager ?? new Pagination();
var totalDirs = Directory.GetDirectories(dir);

if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var totalDirs = Directory.GetDirectories(dir);
foreach (var d in totalDirs)
{
var path = Path.Combine(d, CONVERSATION_FILE);
Expand All @@ -223,6 +228,7 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
if (filter?.Status != null) matched = matched && record.Status == filter.Status;
if (filter?.Channel != null) matched = matched && record.Channel == filter.Channel;
if (filter?.UserId != null) matched = matched && record.UserId == filter.UserId;
if (filter?.TaskId != null) matched = matched && record.TaskId == filter.TaskId;

if (!matched) continue;
records.Add(record);
Expand Down
Loading

0 comments on commit f342498

Please sign in to comment.