diff --git a/Common/Protos/aia.proto b/Common/Protos/aia.proto index e754987..7444119 100644 --- a/Common/Protos/aia.proto +++ b/Common/Protos/aia.proto @@ -7,6 +7,7 @@ package aia; service aiAssistance { rpc SendQuestion (aiaRequest) returns (aiaReply); + rpc imageAnalize (aiaRequest) returns (aiaReply); } message aiaRequest { diff --git a/Infrastructure/Infrastructure/models/Aia.cs b/Infrastructure/Infrastructure/models/Aia.cs index 96e5da8..cd3deb7 100644 --- a/Infrastructure/Infrastructure/models/Aia.cs +++ b/Infrastructure/Infrastructure/models/Aia.cs @@ -27,9 +27,10 @@ namespace AIAss.Protos { "CglhaWEucHJvdG8SA2FpYSJdCgphaWFSZXF1ZXN0EhAKCHF1ZXN0aW9uGAEg", "ASgJEg8KB3Byb21wdHMYAiABKAkSDQoFbW9kZWwYAyABKAkSEAoIYXBpdG9r", "ZW4YBCABKAkSCwoDdXJsGAUgASgJIhsKCGFpYVJlcGx5Eg8KB21lc3NhZ2UY", - "ASABKAkyPgoMYWlBc3Npc3RhbmNlEi4KDFNlbmRRdWVzdGlvbhIPLmFpYS5h", - "aWFSZXF1ZXN0Gg0uYWlhLmFpYVJlcGx5Qg+qAgxBSUFzcy5Qcm90b3NiBnBy", - "b3RvMw==")); + "ASABKAkybgoMYWlBc3Npc3RhbmNlEi4KDFNlbmRRdWVzdGlvbhIPLmFpYS5h", + "aWFSZXF1ZXN0Gg0uYWlhLmFpYVJlcGx5Ei4KDGltYWdlQW5hbGl6ZRIPLmFp", + "YS5haWFSZXF1ZXN0Gg0uYWlhLmFpYVJlcGx5Qg+qAgxBSUFzcy5Qcm90b3Ni", + "BnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { diff --git a/Infrastructure/Infrastructure/models/AiaGrpc.cs b/Infrastructure/Infrastructure/models/AiaGrpc.cs index 9dbeca2..bbb93b0 100644 --- a/Infrastructure/Infrastructure/models/AiaGrpc.cs +++ b/Infrastructure/Infrastructure/models/AiaGrpc.cs @@ -58,6 +58,14 @@ namespace AIAss.Protos { __Marshaller_aia_aiaRequest, __Marshaller_aia_aiaReply); + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + static readonly grpc::Method __Method_imageAnalize = new grpc::Method( + grpc::MethodType.Unary, + __ServiceName, + "imageAnalize", + __Marshaller_aia_aiaRequest, + __Marshaller_aia_aiaReply); + /// Service descriptor public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor { @@ -74,6 +82,12 @@ namespace AIAss.Protos { throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, "")); } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual global::System.Threading.Tasks.Task imageAnalize(global::AIAss.Protos.aiaRequest request, grpc::ServerCallContext context) + { + throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, "")); + } + } /// Client for aiAssistance @@ -123,6 +137,26 @@ namespace AIAss.Protos { { return CallInvoker.AsyncUnaryCall(__Method_SendQuestion, null, options, request); } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual global::AIAss.Protos.aiaReply imageAnalize(global::AIAss.Protos.aiaRequest request, grpc::Metadata headers = null, global::System.DateTime? deadline = null, global::System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken)) + { + return imageAnalize(request, new grpc::CallOptions(headers, deadline, cancellationToken)); + } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual global::AIAss.Protos.aiaReply imageAnalize(global::AIAss.Protos.aiaRequest request, grpc::CallOptions options) + { + return CallInvoker.BlockingUnaryCall(__Method_imageAnalize, null, options, request); + } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual grpc::AsyncUnaryCall imageAnalizeAsync(global::AIAss.Protos.aiaRequest request, grpc::Metadata headers = null, global::System.DateTime? deadline = null, global::System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken)) + { + return imageAnalizeAsync(request, new grpc::CallOptions(headers, deadline, cancellationToken)); + } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual grpc::AsyncUnaryCall imageAnalizeAsync(global::AIAss.Protos.aiaRequest request, grpc::CallOptions options) + { + return CallInvoker.AsyncUnaryCall(__Method_imageAnalize, null, options, request); + } /// Creates a new instance of client from given ClientBaseConfiguration. [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] protected override aiAssistanceClient NewInstance(ClientBaseConfiguration configuration) @@ -137,7 +171,8 @@ namespace AIAss.Protos { public static grpc::ServerServiceDefinition BindService(aiAssistanceBase serviceImpl) { return grpc::ServerServiceDefinition.CreateBuilder() - .AddMethod(__Method_SendQuestion, serviceImpl.SendQuestion).Build(); + .AddMethod(__Method_SendQuestion, serviceImpl.SendQuestion) + .AddMethod(__Method_imageAnalize, serviceImpl.imageAnalize).Build(); } /// Register service method with a service binder with or without implementation. Useful when customizing the service binding logic. @@ -148,6 +183,7 @@ namespace AIAss.Protos { public static void BindService(grpc::ServiceBinderBase serviceBinder, aiAssistanceBase serviceImpl) { serviceBinder.AddMethod(__Method_SendQuestion, serviceImpl == null ? null : new grpc::UnaryServerMethod(serviceImpl.SendQuestion)); + serviceBinder.AddMethod(__Method_imageAnalize, serviceImpl == null ? null : new grpc::UnaryServerMethod(serviceImpl.imageAnalize)); } } diff --git a/Presentation/AIAss/Services/aiAssistanceService.cs b/Presentation/AIAss/Services/aiAssistanceService.cs index 69de178..c112373 100644 --- a/Presentation/AIAss/Services/aiAssistanceService.cs +++ b/Presentation/AIAss/Services/aiAssistanceService.cs @@ -38,6 +38,50 @@ namespace AIAss.Services; reponse="خطا در ارتباط سرور ai"; } + return new aiaReply + { + Message = $" {reponse}" + }; + } + public async override Task imageAnalize(aiaRequest request, ServerCallContext context) + { + using var client = new HttpClient(); + client.DefaultRequestHeaders.Authorization = + new AuthenticationHeaderValue("Bearer", request.Apitoken); + var content = new + { + model = request.Model, + messages = new object[] + { + new { + role = "user", + content = new object[] + { + new { type = "text", text =request.Prompts}, + new { + type = "image_url", + image_url = new { + url = $"data:image/jpeg;base64,{request.Question}" + } + } + } + } + } + }; + var response = await client.PostAsync(request.Url, + new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json")); + string reponse = ""; + if (response.IsSuccessStatusCode) + { + var json = await response.Content.ReadAsStringAsync(); + using var doc = JsonDocument.Parse(json); + reponse = doc.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString(); + } + else + { + reponse = "خطا در ارتباط سرور ai"; + } + return new aiaReply { Message = $" {reponse}" diff --git a/Presentation/AIAss/models/Aia.cs b/Presentation/AIAss/models/Aia.cs index 96e5da8..cd3deb7 100644 --- a/Presentation/AIAss/models/Aia.cs +++ b/Presentation/AIAss/models/Aia.cs @@ -27,9 +27,10 @@ namespace AIAss.Protos { "CglhaWEucHJvdG8SA2FpYSJdCgphaWFSZXF1ZXN0EhAKCHF1ZXN0aW9uGAEg", "ASgJEg8KB3Byb21wdHMYAiABKAkSDQoFbW9kZWwYAyABKAkSEAoIYXBpdG9r", "ZW4YBCABKAkSCwoDdXJsGAUgASgJIhsKCGFpYVJlcGx5Eg8KB21lc3NhZ2UY", - "ASABKAkyPgoMYWlBc3Npc3RhbmNlEi4KDFNlbmRRdWVzdGlvbhIPLmFpYS5h", - "aWFSZXF1ZXN0Gg0uYWlhLmFpYVJlcGx5Qg+qAgxBSUFzcy5Qcm90b3NiBnBy", - "b3RvMw==")); + "ASABKAkybgoMYWlBc3Npc3RhbmNlEi4KDFNlbmRRdWVzdGlvbhIPLmFpYS5h", + "aWFSZXF1ZXN0Gg0uYWlhLmFpYVJlcGx5Ei4KDGltYWdlQW5hbGl6ZRIPLmFp", + "YS5haWFSZXF1ZXN0Gg0uYWlhLmFpYVJlcGx5Qg+qAgxBSUFzcy5Qcm90b3Ni", + "BnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { diff --git a/Presentation/AIAss/models/AiaGrpc.cs b/Presentation/AIAss/models/AiaGrpc.cs index 9dbeca2..bbb93b0 100644 --- a/Presentation/AIAss/models/AiaGrpc.cs +++ b/Presentation/AIAss/models/AiaGrpc.cs @@ -58,6 +58,14 @@ namespace AIAss.Protos { __Marshaller_aia_aiaRequest, __Marshaller_aia_aiaReply); + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + static readonly grpc::Method __Method_imageAnalize = new grpc::Method( + grpc::MethodType.Unary, + __ServiceName, + "imageAnalize", + __Marshaller_aia_aiaRequest, + __Marshaller_aia_aiaReply); + /// Service descriptor public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor { @@ -74,6 +82,12 @@ namespace AIAss.Protos { throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, "")); } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual global::System.Threading.Tasks.Task imageAnalize(global::AIAss.Protos.aiaRequest request, grpc::ServerCallContext context) + { + throw new grpc::RpcException(new grpc::Status(grpc::StatusCode.Unimplemented, "")); + } + } /// Client for aiAssistance @@ -123,6 +137,26 @@ namespace AIAss.Protos { { return CallInvoker.AsyncUnaryCall(__Method_SendQuestion, null, options, request); } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual global::AIAss.Protos.aiaReply imageAnalize(global::AIAss.Protos.aiaRequest request, grpc::Metadata headers = null, global::System.DateTime? deadline = null, global::System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken)) + { + return imageAnalize(request, new grpc::CallOptions(headers, deadline, cancellationToken)); + } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual global::AIAss.Protos.aiaReply imageAnalize(global::AIAss.Protos.aiaRequest request, grpc::CallOptions options) + { + return CallInvoker.BlockingUnaryCall(__Method_imageAnalize, null, options, request); + } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual grpc::AsyncUnaryCall imageAnalizeAsync(global::AIAss.Protos.aiaRequest request, grpc::Metadata headers = null, global::System.DateTime? deadline = null, global::System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken)) + { + return imageAnalizeAsync(request, new grpc::CallOptions(headers, deadline, cancellationToken)); + } + [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] + public virtual grpc::AsyncUnaryCall imageAnalizeAsync(global::AIAss.Protos.aiaRequest request, grpc::CallOptions options) + { + return CallInvoker.AsyncUnaryCall(__Method_imageAnalize, null, options, request); + } /// Creates a new instance of client from given ClientBaseConfiguration. [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)] protected override aiAssistanceClient NewInstance(ClientBaseConfiguration configuration) @@ -137,7 +171,8 @@ namespace AIAss.Protos { public static grpc::ServerServiceDefinition BindService(aiAssistanceBase serviceImpl) { return grpc::ServerServiceDefinition.CreateBuilder() - .AddMethod(__Method_SendQuestion, serviceImpl.SendQuestion).Build(); + .AddMethod(__Method_SendQuestion, serviceImpl.SendQuestion) + .AddMethod(__Method_imageAnalize, serviceImpl.imageAnalize).Build(); } /// Register service method with a service binder with or without implementation. Useful when customizing the service binding logic. @@ -148,6 +183,7 @@ namespace AIAss.Protos { public static void BindService(grpc::ServiceBinderBase serviceBinder, aiAssistanceBase serviceImpl) { serviceBinder.AddMethod(__Method_SendQuestion, serviceImpl == null ? null : new grpc::UnaryServerMethod(serviceImpl.SendQuestion)); + serviceBinder.AddMethod(__Method_imageAnalize, serviceImpl == null ? null : new grpc::UnaryServerMethod(serviceImpl.imageAnalize)); } }