-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nebula AI .NET Integration (Beta) (#122)
- Loading branch information
1 parent
a6afc39
commit 1e59bf6
Showing
10 changed files
with
1,003 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System.Numerics; | ||
using Thirdweb.AI; | ||
|
||
namespace Thirdweb.Tests.AI; | ||
|
||
public class NebulaTests : BaseTests | ||
{ | ||
private const string NEBULA_TEST_CONTRACT = "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8"; | ||
private const string NEBULA_TEST_USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"; | ||
private const int NEBULA_TEST_CHAIN = 11155111; | ||
|
||
public NebulaTests(ITestOutputHelper output) | ||
: base(output) { } | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Create_CreatesSession() | ||
{ | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
Assert.NotNull(nebula); | ||
Assert.NotNull(nebula.SessionId); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Create_ResumesSession() | ||
{ | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var sessionId = nebula.SessionId; | ||
Assert.NotNull(nebula); | ||
Assert.NotNull(nebula.SessionId); | ||
|
||
nebula = await ThirdwebNebula.Create(this.Client, sessionId); | ||
Assert.NotNull(nebula); | ||
Assert.Equal(sessionId, nebula.SessionId); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Chat_Single_ReturnsResponse() | ||
{ | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var response = await nebula.Chat( | ||
message: "What's the symbol of this contract?", | ||
context: new NebulaContext(contractAddresses: new List<string> { NEBULA_TEST_CONTRACT }, chainIds: new List<BigInteger> { NEBULA_TEST_CHAIN }) | ||
); | ||
Assert.NotNull(response); | ||
Assert.NotNull(response.Message); | ||
Assert.Contains("CAT", response.Message); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Chat_Single_NoContext_ReturnsResponse() | ||
{ | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var response = await nebula.Chat(message: $"What's the symbol of this contract: {NEBULA_TEST_CONTRACT} (Sepolia)?"); | ||
Assert.NotNull(response); | ||
Assert.NotNull(response.Message); | ||
Assert.Contains("CAT", response.Message); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Chat_Multiple_ReturnsResponse() | ||
{ | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var response = await nebula.Chat( | ||
messages: new List<NebulaChatMessage> | ||
{ | ||
new("What's the symbol of this contract?", NebulaChatRole.User), | ||
new("The symbol is CAT", NebulaChatRole.Assistant), | ||
new("What's the name of this contract?", NebulaChatRole.User), | ||
}, | ||
context: new NebulaContext(contractAddresses: new List<string> { NEBULA_TEST_CONTRACT }, chainIds: new List<BigInteger> { NEBULA_TEST_CHAIN }) | ||
); | ||
Assert.NotNull(response); | ||
Assert.NotNull(response.Message); | ||
Assert.Contains("CatDrop", response.Message, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Chat_UnderstandsWalletContext() | ||
{ | ||
var wallet = await PrivateKeyWallet.Generate(this.Client); | ||
var expectedAddress = await wallet.GetAddress(); | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var response = await nebula.Chat(message: "What is my wallet address?", wallet: wallet); | ||
Assert.NotNull(response); | ||
Assert.NotNull(response.Message); | ||
Assert.Contains(expectedAddress, response.Message); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Execute_ReturnsMessageAndReceipt() | ||
{ | ||
var signer = await PrivateKeyWallet.Generate(this.Client); | ||
var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN); | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var response = await nebula.Execute( | ||
new List<NebulaChatMessage> | ||
{ | ||
new("What's the address of vitalik.eth", NebulaChatRole.User), | ||
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant), | ||
new("Approve 1 USDC to them", NebulaChatRole.User), | ||
}, | ||
wallet: wallet, | ||
context: new NebulaContext(contractAddresses: new List<string>() { NEBULA_TEST_USDC_ADDRESS }) | ||
); | ||
Assert.NotNull(response); | ||
Assert.NotNull(response.Message); | ||
Assert.NotNull(response.TransactionReceipts); | ||
Assert.NotEmpty(response.TransactionReceipts); | ||
Assert.NotNull(response.TransactionReceipts[0].TransactionHash); | ||
Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66); | ||
} | ||
|
||
[Fact(Timeout = 120000)] | ||
public async Task Execute_ReturnsMessageAndReceipts() | ||
{ | ||
var signer = await PrivateKeyWallet.Generate(this.Client); | ||
var wallet = await SmartWallet.Create(signer, NEBULA_TEST_CHAIN); | ||
var nebula = await ThirdwebNebula.Create(this.Client); | ||
var response = await nebula.Execute( | ||
new List<NebulaChatMessage> | ||
{ | ||
new("What's the address of vitalik.eth", NebulaChatRole.User), | ||
new("The address of vitalik.eth is 0xd8dA6BF26964aF8E437eEa5e3616511D7G3a3298", NebulaChatRole.Assistant), | ||
new("Approve 1 USDC to them", NebulaChatRole.User), | ||
}, | ||
wallet: wallet, | ||
context: new NebulaContext(contractAddresses: new List<string>() { NEBULA_TEST_USDC_ADDRESS }) | ||
); | ||
Assert.NotNull(response); | ||
Assert.NotNull(response.Message); | ||
Assert.NotNull(response.TransactionReceipts); | ||
Assert.NotEmpty(response.TransactionReceipts); | ||
Assert.NotNull(response.TransactionReceipts[0].TransactionHash); | ||
Assert.True(response.TransactionReceipts[0].TransactionHash.Length == 66); | ||
} | ||
} |
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,32 @@ | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Thirdweb.AI; | ||
|
||
internal class ChatClient | ||
{ | ||
private readonly IThirdwebHttpClient _httpClient; | ||
|
||
public ChatClient(IThirdwebHttpClient httpClient) | ||
{ | ||
this._httpClient = httpClient; | ||
} | ||
|
||
public async Task<ChatResponse> SendMessageAsync(ChatParamsSingleMessage message) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(message), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/chat", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ChatResponse>(responseContent); | ||
} | ||
|
||
public async Task<ChatResponse> SendMessagesAsync(ChatParamsMultiMessages messages) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(messages), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/chat", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ChatResponse>(responseContent); | ||
} | ||
} |
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,32 @@ | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Thirdweb.AI; | ||
|
||
internal class ExecutionClient | ||
{ | ||
private readonly IThirdwebHttpClient _httpClient; | ||
|
||
public ExecutionClient(IThirdwebHttpClient httpClient) | ||
{ | ||
this._httpClient = httpClient; | ||
} | ||
|
||
public async Task<ChatResponse> ExecuteAsync(ChatParamsSingleMessage command) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(command), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/execute", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ChatResponse>(responseContent); | ||
} | ||
|
||
public async Task<ChatResponse> ExecuteBatchAsync(ChatParamsMultiMessages commands) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(commands), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/execute", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ChatResponse>(responseContent); | ||
} | ||
} |
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,28 @@ | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Thirdweb.AI; | ||
|
||
internal class FeedbackClient | ||
{ | ||
private readonly IThirdwebHttpClient _httpClient; | ||
|
||
public FeedbackClient(IThirdwebHttpClient httpClient) | ||
{ | ||
this._httpClient = httpClient; | ||
} | ||
|
||
/// <summary> | ||
/// Submits feedback for a specific session and request. | ||
/// </summary> | ||
/// <param name="feedback">The feedback parameters to submit.</param> | ||
/// <returns>The submitted feedback details.</returns> | ||
public async Task<Feedback> SubmitFeedbackAsync(FeedbackParams feedback) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(feedback), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/feedback", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<Feedback>>(responseContent).Result; | ||
} | ||
} |
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,64 @@ | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Thirdweb.AI; | ||
|
||
internal class SessionManager | ||
{ | ||
private readonly IThirdwebHttpClient _httpClient; | ||
|
||
public SessionManager(IThirdwebHttpClient httpClient) | ||
{ | ||
this._httpClient = httpClient; | ||
} | ||
|
||
public async Task<List<SessionList>> ListSessionsAsync() | ||
{ | ||
var response = await this._httpClient.GetAsync($"{Constants.NEBULA_API_URL}/session/list"); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<List<SessionList>>>(content).Result; | ||
} | ||
|
||
public async Task<Session> GetSessionAsync(string sessionId) | ||
{ | ||
var response = await this._httpClient.GetAsync($"{Constants.NEBULA_API_URL}/session/{sessionId}"); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<Session>>(content).Result; | ||
} | ||
|
||
public async Task<Session> CreateSessionAsync(CreateSessionParams parameters) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/session", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<Session>>(responseContent).Result; | ||
} | ||
|
||
public async Task<Session> UpdateSessionAsync(string sessionId, UpdateSessionParams parameters) | ||
{ | ||
var content = new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json"); | ||
var response = await this._httpClient.PutAsync($"{Constants.NEBULA_API_URL}/session/{sessionId}", content); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<Session>>(responseContent).Result; | ||
} | ||
|
||
public async Task<SessionDeleteResponse> DeleteSessionAsync(string sessionId) | ||
{ | ||
var response = await this._httpClient.DeleteAsync($"{Constants.NEBULA_API_URL}/session/{sessionId}"); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<SessionDeleteResponse>>(content).Result; | ||
} | ||
|
||
public async Task<Session> ClearSessionAsync(string sessionId) | ||
{ | ||
var response = await this._httpClient.PostAsync($"{Constants.NEBULA_API_URL}/session/{sessionId}/clear", null); | ||
_ = response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<ResponseModel<Session>>(content).Result; | ||
} | ||
} |
Oops, something went wrong.