Anul 3 Semestrul 2

This commit is contained in:
2025-07-03 20:56:38 +03:00
parent 184f3bd92e
commit 3b7fb85767
269 changed files with 20955 additions and 0 deletions
+106
View File
@@ -0,0 +1,106 @@
A = [1,2;3,4]
det(A)
inv(A)*A
A*A
A.*A
A^2
A.^2
v=1:10
V=1:-0.1:0
v.^2
transpose(v) # or v'
A(1,:)
A(:,2)
#L1_NC.pdf
#1.a
x=-4:0.1:7.2;
p = x.^5-5*x.^4-16*x.^3+16*x.^2-17.*x+21;
#plot(x,p)
#1.b
x=-2.5;
p = [1,-5,-16,+16,-17,21];
polyval(p,x)
#1.c
roots(p)
polyval(p,7)
#2
x = 0:0.1*pi:2*pi;
f = sin(x);
g = sin(2*x);
h = sin(3*x);
#subplot(3,1,1)
#plot(x,f)
#subplot(3,1,2)
#plot(x,g)
#subplot(3,1,3)
#plot(x,h)
clf #clear plot
t= 0:0.1*pi:10*pi
R=3.8;
r=1;
x = (R+r)*cos(t) - r*cos((R/r+1)*t);
y = (R+r)*sin(t) - r*sin((R/r+1)*t);
plot(x,y)
[x, y] = meshgrid(-2:0.1:2, 0.5:0.1:4.5);
f = sin(e.^x).*cos(log(y));
clf
#mesh(x, y, f);
#xlabel('X-axis');
#ylabel('Y-axis');
#zlabel('Z-axis');
#title('3D Surface Plot using mesh');
#colormap('jet');
#colorbar;
#grid on;
figure;
plot3(x, y, f);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Line Plot using plot3');
grid on;
function result = funct(n)
if n == 0
result = 1+1;
else
result = 1 + 1/ funct(n - 1);
end
end
% Increase the recursion limit to 5000
#max_recursion_depth(2025);
funct(2)
funct(10)
funct(100)
funct(2025)
@@ -0,0 +1,9 @@
f=@(x)1./x;
R = rectangle_int(f,1,2,100)
R = trapz_int(f,1,2,100)
R = homer_int(f,1,2,100)
+25
View File
@@ -0,0 +1,25 @@
#f=@(x)((2./sqrt(pi)).*(e.^(-x.^2)));
#for b=0.1:0.1:1
# I = adquad(f,0,b,0.1,4)
# I2 = integral(f,0,b)
#end
f=@(x)(sin(x.^(1/3)));
g=@(x)(sin(x).*3.*(x.^2));
x = [2,4,8,16,32,64,128,256];
trueI = integral(f,0,1)
for n = x
n
I1=homer_int(f,0,1,n)
fprintf('%.*f \n', 10, abs(trueI-I1))
I2=homer_int(g,0,1,n)
fprintf('%.*f \n', 10, abs(trueI-I2))
end
+80
View File
@@ -0,0 +1,80 @@
#pkg load symbolic;
#1.a
syms x;
f=exp(x)
t1=taylor(f,x,0,'order',1);
t2=taylor(f,x,0,'order',2);
t3=taylor(f,x,0,'order',3);
t4=taylor(f,x,0,'order',4);
t10 = taylor(f,x,0,'order',10);
#ezplot(t1)
hold on
ezplot(t2)
hold on
ezplot(t3)
hold on
ezplot(t4)
xlim([-3,3])
#1.b
vpa(exp(1),7)
vpa(subs(t10,x,1),7)
#2.a
syms x;
f=sin(x)
t3=taylor(f,x,0,'order',3);
t5=taylor(f,x,0,'order',5);
hold off
ezplot(f)
hold on
ezplot(t3)
hold on
ezplot(t5)
xlim([-pi,pi])
ylim([-2,2])
#2.b
t10=taylor(f,x,0,'order',10);
vpa(sin(pi/5),5)
vpa(subs(t10,x,sym(pi)/5),5)
vpa(sin(10*pi/3),5)
vpa(subs(t10,x,10*sym(pi)/3),5) #it's not precise enought, increase n for Taylor series or move x0=0 to x0= 10pi/3
#3.a
syms x;
f=log(1+x);
t2= taylor(f,x,0,'order',2);
t5= taylor(f,x,0,'order',5);
hold off
ezplot(f)
hold on
ezplot(t2)
hold on
ezplot(t5)
xlim([-0.9,1])
ylim([-1,1])
t= taylor(f,x,0,'order',10);
vpa(log(2),5)
vpa(subs(t,x,1),5)
syms x;
g=log(1-x);
t2 = taylor(g,x,0,'order',10)
t-t2
vpa(subs(t,x,sym(0.999,'f')) - subs(g,x,sym(0.999,'f')),5)
+30
View File
@@ -0,0 +1,30 @@
x = [0 1 2];
f = 1./(1+x);
t=divdiff(x,f);
x = [0, 1, 2];
f = 1./(1+x);
df = (-1)./((1+x).^2);
[z,t]=divdiff2(x,f,df)
x=linspace(1,2,11);
f=1./(1+x);
df = (-1)./((1+x).^2);
t=divdiff(x,f)
[z,t]=divdiff2(x,f,df)
x = [-2, -1, 0, 1, 2, 3, 4];
f = [-5, 1, 1, 1,7 ,25, 60];
t=divdiff(x,f)
x = [-2, -1, 0, 1, 2, 3, 4];
f = [-5, 1, 1, 1,7 ,25, 60];
t=forwarddiff(f)
t=backdiff(f)
+26
View File
@@ -0,0 +1,26 @@
#U= [2,4,2;0,-1,1;0,0,-1];
#b = [8;0;-1];
#x = backsub(U,b);
A= [2 1 -1 -2; 4 4 1 3; -6 -1 10 10; -2 1 8 4];
b = [2;4;-5;1];
x = gauss(A,b);
n=5
A=5*eye(n) -diag(ones(1,n-1),1)- diag(ones(1,n-1),-1)
b = [4,3* ones(1,n-2),4]'
[L,U,P]= lu(A);
y = forwsub(L,P*b)
x = backsub(U, y)
+42
View File
@@ -0,0 +1,42 @@
n=7;
A=5*eye(n) -diag(ones(1,n-1),1)- diag(ones(1,n-1),-1);
b = [4,3* ones(1,n-2),4]';
x0 = zeros(size(b));
[x, nit] = jacobi(A,b, x0,10^(-5), 1000);
[x, nit] = gaussseidel(A,b, x0,10^(-5), 1000);
A = [10 7 8 7; 7 5 6 5; 8 6 10 9; 7 5 9 10];
b=[32;23;33;31];
x = A^-1*b
trueB = b;
trueX = x;
trueA = A;
A = [10 7 8 7; 7 5 6 5; 8 6 10 9; 7 5 9 10];
b=[32.1;22.9;33.1;30.9];
x = A^-1*b
norm(trueX-x,inf)/norm(trueX,inf)
norm(trueB-b,inf)/norm(trueB,inf)
cond(A)
A = [10 7 8.1 7.2; 7.8 5.04 6 5; 8 5.98 9.89 9; 6.99 4.99 9 9.98];
b=[32;23;33;31];
x = A^-1*b
cond(A)
norm(trueX-x,inf)/norm(trueX,inf)
norm(trueA-A,inf)/norm(trueA,inf)
+39
View File
@@ -0,0 +1,39 @@
#1.b
nodes = linspace(-2,4,10);
f=@(x)(x+1)./(3*x.^2+2*x+1);
plot(nodes, f(nodes),'o');
hold on
x=linspace(-2,4,500);
plot(x,f(x));
hold on
lnf=lagrange_classic(nodes,f(nodes),x);
plot(x,lnf)
plot(x,abs(f(x)-lnf),'x')
nodes = [81,100,121];
f_nodes = [9,10,11];
sqrt(118);
lnf = lagrange_classic(nodes,f_nodes, [118]);
nodes = [1980,1990,2000,2010,2020];
f_nodes = [4451,5287,6090,6970,7821];
lnf = lagrange_barycentric(nodes, f_nodes, [2005,2015])
abs([6474,7405] - lnf)
+32
View File
@@ -0,0 +1,32 @@
#truex = linspace(0,1,20);
#x0=[0,1/3,1/2,1]
#f=@(x)cos(pi*x);
#plot(x0, f(x0),'o');
#hold on
#plot(truex, f(truex),'blue');
#lnf = newton(x0,f(x0),truex)
#plot(truex, lnf, 'r');
#lnf = newton(x0,f(x0),1/5)
#truex = linspace(1001,1009,9);
#x0=[1000,1010,1020,1030,1040,1050];
#f0=[3.000000,3.0043214,3.0086002,3.0128372,3.0170333,3.0211893];
#lnf = newton(x0,f0,truex)
x0 = linspace(-4,4,9);
f=@(x)power(2,x);
lnf = aitken(x0,f(x0),1/2)
+63
View File
@@ -0,0 +1,63 @@
nodes = linspace(-2,4,7);
f=@(x)(x+1)./(3*x.^2+2*x+1);
#plot(nodes, f(nodes),'o');
#hold on
x=linspace(-2,4,500);
#plot(x,f(x));
hold on
lnf=lagrange_classic(nodes,f(nodes),x);
#plot(x,lnf)
boor = spline(nodes,f(nodes),x);
#plot(x,boor)
df = @(x)(-(3*x.^2+6*x+1)./(3*x.^2+2*x+1).^2);
herm = hermite(nodes,f(nodes), df(nodes),x)
#plot(x,herm)
f=@(x)(x.*sin(pi.*x));
nodes = [-1,-1/2,0,1/2,1,3/2];
x=linspace(-1,3/2,500);
boor = spline(nodes,f(nodes),x);
boor_comp = spline(nodes,[pi,f(nodes),-1],x)
piece = pchip(nodes,f(nodes),x)
#plot(nodes, f(nodes),'o');
#hold on
#plot(x,boor);
#plot(x, boor_comp);
#plot(x, piece);
nodes = [0.5,1.5,2,3,3.5,4.5,5,6,7,8];
f_nodes = [5,5.8,5.8,6.8,6.9,7.6,7.8,8.2,9.2,9.9];
x=linspace(0.5,8,500);
p = polyfit(nodes,f_nodes,1);
plot(nodes, f_nodes,'o');
hold on
plot(x, polyval(p,x))
norm(p)
polyval(p,4)
@@ -0,0 +1,10 @@
function I = adquad(f, a, b, err, m)
I1 = trapz_int(f, a, b, m);
I2 = trapz_int(f, a, b, 2*m);
if abs(I1 - I2) < err % success
I = I2;
return
else % recursive subdivision
I = adquad(f, a, (a+b)/2, err, m) + adquad(f, (a+b)/2, b, err, m);
end
end;
@@ -0,0 +1,12 @@
function Lnf = aitken(x0,f0,x)
p = zeros(length(x0));
p(:,1) = f0'
n = length(x0)
for i=2:n
for j=2:i
p(i,j) = (1/(x0(i)-x0(i-j+1)))*det([x-x0(i-j+1), p(i-1,j-1);x-x0(i),p(i,j-1)]);
endfor
endfor
Lnf=p(n,n);
end;
@@ -0,0 +1,11 @@
function t = backdiff(f)
n = length(f);
t = zeros(n);
t(:,1)= f'
for j = 2:n
t(j:n,j) = diff(t(j-1:n,j-1));
endfor
end;
@@ -0,0 +1,10 @@
function x = backsub(U, b)
n = length(b);
x = zeros(size(b));
for k = n:-1:1
x(k) = (b(k) - (U(k,k+1:n) * x(k+1:n)))/U(k,k);
endfor
end;
@@ -0,0 +1,11 @@
function t = divdiff(x, f)
n = length(x);
t=zeros(n);
t(:,1)= f'
for j = 2:n
t(1:n-j+1,j) = (t(2:n-j+2,j-1) - t(1:n-j+1,j-1))./((x(j:n)-x(1:n-j+1))');
endfor
end
@@ -0,0 +1,13 @@
function [z, t] = divdiff2(x,f,df)
z = repelem(x,2);
lz = length(z);
f2 = repelem(f,2);
t=zeros(lz);
t(:,1)= f2';
t(1:2:lz-1,2) = df';
t(2:2:lz-2,2) = (diff(f)./diff(x))';
for j = 3:lz
t(1:lz-j+1,j) = (t(2:lz-j+2,j-1) - t(1:lz-j+1,j-1))./((z(j:lz)-z(1:lz-j+1))');
endfor
end
@@ -0,0 +1,11 @@
function t = forwarddiff(f)
n = length(f);
t = zeros(n);
t(:,1)= f'
for j = 2:n
t(1:n-j+1,j) = diff(t(1:n-j+2,j-1));
endfor
end;
@@ -0,0 +1,10 @@
function x = forwsub(U, b)
n = length(b);
x = zeros(size(b));
for k = 1:n
x(k) = (b(k) - (U(k,1:k-1) * x(1:k-1)))/U(k,k);
endfor
end;
+18
View File
@@ -0,0 +1,18 @@
function x = gauss(A, b)
[r,n] = size(A);
x = zeros(size(b));
A = [A,b]
for i = 1:n
[v, p] = max(abs(A(i:n, i)));
p = p + i - 1;
A([i p],:) = A([p i],:);
for j = i+1:n
coeff = A(j, i)/ A(i,i);
A(j, i:n+1) = A(j, i:n+1) - coeff * A(i,i:n+1)
endfor
endfor
x = backsub(A(:,1:n),A(:,n+1));
end;
@@ -0,0 +1,20 @@
function [x, nit] = gausssidel(A, b, x0, err, maxnit)
n = length(b);
D = diag(diag(A));
L = tril(A, -1);
U = A - D - L;
M = D + L;
N = -U;
T = M^(-1)*N;
c = M^(-1)*b;
x = x0;
for k = 1:maxnit
old_x = x;
x = T*x+c;
if norm(x-old_x,inf) <= ((1-norm(T,inf))/norm(T,inf))*err
nit = k;
return;
endif
endfor
error('Too dificult');
end;
@@ -0,0 +1,13 @@
function Hnf = hermite(x0,f0,df0,x)
[z,dz] = divdiff2(x0,f0,df0);
x0=z;
ds = dz(1,:);
n = length(ds);
m = length(x);
Hnf = zeros(size(x));
for i=1:m
for j=1:n
Hnf(i) = Hnf(i) + ds(j) * (prod( x(i) - x0([1:j-1])));
endfor
endfor
end;
@@ -0,0 +1,4 @@
function R = homer_int(f, a, b, n)
R = ((b-a)/(2*3*n)) * (f(a)+f(b)+4*sum(f(a+([1:2:2*n-1])*((b-a)/(2*n))))+2*sum(f(a+([2:2:2*n-2])*((b-a)/(2*n)))));
end;
@@ -0,0 +1,17 @@
function [x, nit] = jacobi(A, b, x0, err, maxnit)
n = length(b);
M = diag(diag(A));
N = M - A;
T = M^(-1)*N;
c = M^(-1)*b;
x = x0;
for k = 1:maxnit
old_x = x;
x = T*x+c;
if norm(x-old_x,inf) <= ((1-norm(T,inf))/norm(T,inf))*err
nit = k;
return;
endif
endfor
error('Too dificult');
end;
@@ -0,0 +1,14 @@
function Lnf = lagrange_barycentric(nodes,f_nodes,x)
n = length(nodes);
m = length(x);
Lnf = zeros(size(x));
l = zeros(size(nodes));
for i = 1:m
ux = prod( x(i) - nodes([1:n]));
for j = 1:n
wi(j) = 1/prod( nodes(j) - nodes([1:j-1,j+1:n]));
endfor
Lnf(i) = ux* ((wi./(x(i) - nodes([1:n])))*f_nodes');
endfor
end;
@@ -0,0 +1,13 @@
function Lnf = lagrange_classic(nodes,f_nodes,x)
n = length(nodes);
m = length(x);
Lnf = zeros(size(x));
l = zeros(size(nodes));
for i = 1:m
for j = 1:n
l(j) =(prod( x(i) - nodes([1:j-1,j+1:n])))/(prod( nodes(j) - nodes([1:j-1,j+1:n])));
endfor
Lnf(i) = l*f_nodes';
endfor
end;
@@ -0,0 +1,13 @@
function Lnf = newton(x0,f0,x)
d = divdiff(x0,f0);
ds = d(1,:);
n = length(ds);
m = length(x);
Lnf = zeros(size(x));
for i=1:m
for j=1:n
Lnf(i) = Lnf(i) + ds(j) * (prod( x(i) - x0([1:j-1])));
endfor
endfor
end;
@@ -0,0 +1,4 @@
function R = rectangle_int(f, a, b, n)
R = ((b-a)/n) * sum(f(a+([0:n-1]+1/2)*((b-a)/n)))
end;
@@ -0,0 +1,4 @@
function R = trapz_int(f, a, b, n)
R = ((b-a)/(2*n)) * (f(a)+f(b)+2*sum(f(a+([1:n-1])*((b-a)/n))));
end;
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,57 @@
using System;
using FileCompression.Enums;
using FileCompression.Services;
using FileCompression.Services.Strategies;
namespace FileCompression;
public class Compressor
{
private static Compressor? _compressor;
private static readonly Lock _lock = new();
private static readonly StrategyContext StrategyContext = new();
private Compressor()
{
}
public static Compressor Instance
{
get
{
lock (_lock)
{
if (_compressor == null)
{
_compressor = new Compressor();
}
return _compressor;
}
}
}
public void Compress(string sourcePath, string destinationPath, CompressionMode mode)
{
if (string.IsNullOrEmpty(sourcePath))
{
throw new ArgumentNullException(nameof(sourcePath), "Source path cannot be null or empty.");
}
if (string.IsNullOrEmpty(destinationPath))
{
throw new ArgumentNullException(nameof(destinationPath), "Destination path cannot be null or empty.");
}
if (File.Exists(destinationPath))
{
File.Delete(destinationPath);
}
StrategyContext.SetStrategy(CompressorFactory.CreateCompressorStrategy(mode));
var compressor = CompressorFactory.CreateCompressor(sourcePath, destinationPath, mode, StrategyContext);
compressor.Compress();
Console.WriteLine($"Compressed {sourcePath} to {destinationPath} using {mode} mode.");
Console.WriteLine($"Compression completed successfully.");
}
}
@@ -0,0 +1,8 @@
namespace FileCompression.Enums;
public enum CompressionMode
{
Zip,
Tar,
TarGz
}
@@ -0,0 +1,7 @@
namespace FileCompression.Enums;
public enum EntryType
{
File,
Directory
}
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,9 @@
using System;
namespace FileCompression.Interfaces;
public interface ICompressionStrategy
{
void CompressFile(string sourcePath, string destinationPath, string basePath);
void CompressDirectory(string sourcePath, string destinationPath, string basePath);
}
@@ -0,0 +1,11 @@
using System;
using System.IO.Compression;
using FileCompression.Services.Strategies;
namespace FileCompression.Interfaces;
public interface ICompressor
{
void Compress();
}
@@ -0,0 +1,10 @@
using System;
using FileCompression.Enums;
namespace FileCompression.Models;
public class Entry
{
public required string Path { get; set; }
public EntryType Type { get; set; }
}
@@ -0,0 +1,25 @@
using System;
using FileCompression.Interfaces;
using FileCompression.Services.Strategies;
namespace FileCompression.Services.Composites;
public class FileCompressor : ICompressor
{
private string sourcePath;
private string destinationPath;
private string basePath;
private StrategyContext strategyContext;
public FileCompressor(string sourcePath, string destinationPath, string basePath, StrategyContext strategyContext)
{
this.sourcePath = sourcePath;
this.destinationPath = destinationPath;
this.strategyContext = strategyContext;
this.basePath = basePath;
}
public void Compress()
{
strategyContext.CompressFile(sourcePath, destinationPath, basePath);
}
}
@@ -0,0 +1,48 @@
using System;
using FileCompression.Enums;
using FileCompression.Interfaces;
using FileCompression.Models;
using FileCompression.Services.Strategies;
namespace FileCompression.Services.Composites;
public class FolderCompressor : ICompressor
{
private readonly string sourcePath;
private readonly string destinationPath;
private readonly StrategyContext strategyContext;
private readonly string basePath;
public FolderCompressor(string sourcePath, string destinationPath, string basePath, StrategyContext strategyContext)
{
this.sourcePath = sourcePath;
this.destinationPath = destinationPath;
this.strategyContext = strategyContext;
this.basePath = basePath;
}
public void Compress()
{
strategyContext.CompressDirectory(sourcePath, destinationPath, basePath);
var entries = GetEntries();
foreach (var entry in entries)
{
entry.Compress();
}
}
private List<ICompressor> GetEntries()
{
List<ICompressor> entries = [];
foreach (var file in Directory.GetFiles(sourcePath))
{
entries.Add(new FileCompressor(file, destinationPath, basePath, strategyContext));
}
foreach (var directory in Directory.GetDirectories(sourcePath))
{
entries.Add(new FolderCompressor(directory, destinationPath, basePath, strategyContext));
}
return entries;
}
}
@@ -0,0 +1,43 @@
using System;
using System.IO.Compression;
using FileCompression.Interfaces;
using FileCompression.Services.Strategies;
namespace FileCompression.Services.Composites;
public class TarCompressor : ICompressor
{
private readonly string sourcePath;
private readonly string destinationPath;
private readonly StrategyContext strategyContext;
private readonly string basePath;
private readonly string tarPath;
private ICompressor compressor;
public TarCompressor(string sourcePath, string destinationPath, string basePath, StrategyContext strategyContext)
{
this.sourcePath = sourcePath;
this.destinationPath = destinationPath;
this.strategyContext = strategyContext;
this.basePath = basePath;
this.tarPath = destinationPath + ".tar";
compressor = File.Exists(sourcePath) ? new FileCompressor(sourcePath, tarPath, basePath, strategyContext) : new FolderCompressor(sourcePath, tarPath, basePath, strategyContext);
}
public void Compress()
{
compressor.Compress();
GZipCompress();
}
private void GZipCompress()
{
using (var fileStream = new FileStream(tarPath, FileMode.Open))
using (var compressedFileStream = File.Create(destinationPath))
using (var compressionStream = new GZipStream(compressedFileStream, CompressionLevel.Optimal))
{
fileStream.CopyTo(compressionStream);
}
File.Delete(tarPath);
}
}
@@ -0,0 +1,59 @@
using System;
using FileCompression.Enums;
using FileCompression.Interfaces;
using FileCompression.Services.Composites;
using FileCompression.Services.Strategies;
namespace FileCompression.Services;
public static class CompressorFactory
{
public static ICompressionStrategy CreateCompressorStrategy(CompressionMode mode)
{
return mode switch
{
CompressionMode.Zip => new ZipStrategy(),
CompressionMode.Tar => new TarGzStrategy(),
CompressionMode.TarGz => new TarGzStrategy(),
_ => throw new NotSupportedException($"Compression mode '{mode}' is not supported.")
};
}
public static ICompressor CreateCompressor(string sourcePath, string destinationPath, CompressionMode mode, StrategyContext strategyContext)
{
string basePath = sourcePath.Substring(0, sourcePath.LastIndexOf(Path.DirectorySeparatorChar));
basePath = basePath.Substring(0, sourcePath.LastIndexOf(Path.DirectorySeparatorChar));
Console.WriteLine($"Base path: {basePath}");
if (File.Exists(sourcePath))
{
switch (mode)
{
case CompressionMode.Zip:
return new FileCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.Tar:
return new FileCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.TarGz:
return new TarCompressor(sourcePath, destinationPath, basePath, strategyContext);
default:
throw new NotSupportedException($"Compression mode '{mode}' is not supported.");
}
}
if (Directory.Exists(sourcePath))
{
switch (mode)
{
case CompressionMode.Zip:
return new FolderCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.Tar:
return new FolderCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.TarGz:
return new TarCompressor(sourcePath, destinationPath, basePath, strategyContext);
default:
throw new NotSupportedException($"Compression mode '{mode}' is not supported.");
}
}
throw new ArgumentException("Source path must be a file or directory.", nameof(sourcePath));
}
}
@@ -0,0 +1,36 @@
using System;
using FileCompression.Interfaces;
namespace FileCompression.Services.Strategies;
public class StrategyContext
{
private ICompressionStrategy? _compressionStrategy;
public StrategyContext()
{
}
public StrategyContext(ICompressionStrategy compressionStrategy)
{
_compressionStrategy = compressionStrategy;
}
public void SetStrategy(ICompressionStrategy compressionStrategy)
{
_compressionStrategy = compressionStrategy;
}
public void CompressFile(string sourcePath, string destinationPath, string basePath)
{
if (_compressionStrategy == null)
{
throw new InvalidOperationException("Compression strategy is not set.");
}
_compressionStrategy.CompressFile(sourcePath, destinationPath, basePath);
}
public void CompressDirectory(string sourcePath, string destinationPath, string basePath)
{
if (_compressionStrategy == null)
{
throw new InvalidOperationException("Compression strategy is not set.");
}
_compressionStrategy.CompressDirectory(sourcePath, destinationPath, basePath);
}
}
@@ -0,0 +1,68 @@
using System;
using System.Formats.Tar;
using FileCompression.Interfaces;
using FileCompression.Utils;
namespace FileCompression.Services.Strategies;
public class TarGzStrategy : ICompressionStrategy
{
public void CompressFile(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing {sourcePath} to {destinationPath} using tar and gzip.");
string tempPath = Path.GetTempFileName();
using (var tempStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
using (var tempWriter = new TarWriter(tempStream, TarEntryFormat.Pax))
{
// Copy existing entries to temp file
if (File.Exists(destinationPath))
{
using (var originalStream = new FileStream(destinationPath, FileMode.Open, FileAccess.Read))
using (var tarReader = new TarReader(originalStream, leaveOpen: false))
{
TarEntry? entry;
while ((entry = tarReader.GetNextEntry()) is not null)
{
tempWriter.WriteEntry(entry);
}
}
}
// Add new entry
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length));
tempWriter.WriteEntry(sourcePath, entryName);
}
File.Copy(tempPath, destinationPath, overwrite: true);
File.Delete(tempPath);
}
public void CompressDirectory(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing {sourcePath} to {destinationPath} using tar and gzip.");
string tempPath = Path.GetTempFileName();
using (var tempStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
using (var tempWriter = new TarWriter(tempStream, TarEntryFormat.Pax))
{
// Copy existing entries to temp file
if (File.Exists(destinationPath))
{
using (var originalStream = new FileStream(destinationPath, FileMode.Open, FileAccess.Read))
using (var tarReader = new TarReader(originalStream, leaveOpen: false))
{
TarEntry? entry;
while ((entry = tarReader.GetNextEntry()) is not null)
{
tempWriter.WriteEntry(entry);
}
}
}
// Add new entry
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length), appendPathSeparator: true);
tempWriter.WriteEntry(sourcePath, entryName);
}
}
}
@@ -0,0 +1,72 @@
using System;
using FileCompression.Interfaces;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using FileCompression.Utils;
namespace FileCompression.Services.Strategies;
public class ZipStrategy : ICompressionStrategy
{
public void CompressFile(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing file {sourcePath} to {destinationPath}");
using (Stream destination = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using ZipArchive archive = new ZipArchive(destination, ZipArchiveMode.Update);
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length));
DoCreateEntryFromFile(archive, sourcePath, entryName, CompressionLevel.Optimal);
}
}
public void CompressDirectory(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing directory {sourcePath} to {destinationPath}");
using (Stream destination = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using ZipArchive archive = new ZipArchive(destination, ZipArchiveMode.Update);
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length), appendPathSeparator: true);
archive.CreateEntry(entryName);
}
}
private ZipArchiveEntry DoCreateEntryFromFile(ZipArchive destination,
string sourceFileName, string entryName, CompressionLevel? compressionLevel)
{
ArgumentNullException.ThrowIfNull(destination);
ArgumentNullException.ThrowIfNull(sourceFileName);
ArgumentNullException.ThrowIfNull(entryName);
// Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
// as it is a pluggable component that completely encapsulates the meaning of compressionLevel.
// Argument checking gets passed down to FileStream's ctor and CreateEntry
using (FileStream fs = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false))
{
ZipArchiveEntry entry = compressionLevel.HasValue
? destination.CreateEntry(entryName, compressionLevel.Value)
: destination.CreateEntry(entryName);
DateTime lastWrite = File.GetLastWriteTime(sourceFileName);
// If file to be archived has an invalid last modified time, use the first datetime representable in the Zip timestamp format
// (midnight on January 1, 1980):
if (lastWrite.Year < 1980 || lastWrite.Year > 2107)
lastWrite = new DateTime(1980, 1, 1, 0, 0, 0);
entry.LastWriteTime = lastWrite;
using (Stream es = entry.Open())
fs.CopyTo(es);
return entry;
}
}
}
@@ -0,0 +1,26 @@
using System;
namespace FileCompression.Utils;
public static class ArchiveUtils
{
public static string EntryFromPath(ReadOnlySpan<char> path, bool appendPathSeparator = false)
{
// Remove leading separators.
int nonSlash = path.IndexOfAnyExcept('/');
if (nonSlash < 0)
{
nonSlash = path.Length;
}
path = path.Slice(nonSlash);
// Append a separator if necessary.
return (path.IsEmpty, appendPathSeparator) switch
{
(false, false) => path.ToString(),
(false, true) => string.Concat(path, "/"),
(true, false) => string.Empty,
(true, true) => "/",
};
}
}
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../FileCompression/FileCompression.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,34 @@
// See https://aka.ms/new-console-template for more information
using System;
using FileCompression.Interfaces;
using FileCompression.Services.Strategies;
using FileCompression.Services.Composites;
using FileCompression;
using FileCompression.Enums;
// dotnet run --project FileCompressionCLI/ /home/danielcujba/Pictures/Screenshots/ /home/danielcujba/Downloads/destination.zip Zip
// dotnet run --project FileCompressionCLI/ /home/danielcujba/Pictures/Screenshots/ /home/danielcujba/Downloads/destination.tar Tar
// dotnet run --project FileCompressionCLI/ /home/danielcujba/Pictures/Screenshots/ /home/danielcujba/Downloads/destination.tar.gz TarGz
namespace FileCompressionCLI
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: FileCompressionCLI <sourcePath> <destinationPath> <compressionMode>");
return;
}
string sourcePath = args[0];
string destinationPath = args[1];
if (!Enum.TryParse(args[2], true, out CompressionMode mode))
{
Console.WriteLine($"Invalid compression mode: {args[2]}");
return;
}
Compressor.Instance.Compress(sourcePath, destinationPath, mode);
}
}
}
+528
View File
@@ -0,0 +1,528 @@
import openai
import json
import chromadb # For ChromaDB
import os # For checking if requirements file exists
import hashlib # For creating deterministic IDs
# --- Debug Configuration ---
DEBUG_LOGS = True # Set to False to hide detailed step-by-step logs
# Configure the OpenAI client to connect to LM Studio
client = openai.OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
# --- Model Configuration ---
ESB_MODEL_ID = "mistral-7b-instruct-v0.3"
FEEDBACK_MODEL_ID = "phi-3-mini-4k-instruct"
GUARDRAIL_MODEL_ID = "qwen1.5-1.8b-chat"
EMBEDDING_MODEL_ID = "text-embedding-nomic-embed-text-v1.5"
# --- RAG Configuration with ChromaDB ---
REQUIREMENTS_FILE_PATH = "requirements.txt"
CHROMA_DB_PATH = "chroma_db_store" # Path where ChromaDB will store its data
CHROMA_COLLECTION_NAME = "esb_requirements_collection" # Changed name slightly for clarity
chroma_collection = None # Will be initialized
# --- Prompt Engineering ---
# ESB_SYSTEM_PROMPT, FEEDBACK_LLM_SYSTEM_PROMPT, GUARDRAIL_LLM_SYSTEM_PROMPT
# remain the same as in the previous complete script version.
ESB_SYSTEM_PROMPT = """
You are an Emotional Support Buddy (ESB). Your primary goal is to offer decent, empathetic advice or praise.
Listen attentively to the user. Be kind, understanding, and supportive.
Avoid giving medical or financial advice. Focus on emotional well-being.
Keep your responses concise but warm.
"""
FEEDBACK_LLM_SYSTEM_PROMPT = """
You are a specialized AI that outputs a SINGLE-LINE JSON object. This JSON object is a directive for an Emotional Support Buddy (ESB) LLM.
Your SOLE output MUST be a valid JSON object on a single line. Do NOT use conversational language or markdown code fences.
The JSON object MUST have a top-level key named "directive".
The value of "directive" MUST be an object containing these EXACT three sub-keys:
1. "action_type": (string) A concise verb or verb phrase for the ESB's primary action.
Examples: "Congratulate", "AcknowledgeFeelings", "ExpressSympathy", "OfferSupport", "ValidateAndExplore", "PositiveReinforcement".
2. "suggested_tone": (string) Adjectives describing the ESB's desired tone.
Examples: "Enthusiastic and celebratory", "Warm, understanding, and encouraging", "Very gentle, compassionate, and patient", "Supportive and calm".
3. "focus_hint": (string) A BRIEF note (NOT a full message) on what the ESB should address or be mindful of. This helps guide the ESB's unique response.
Examples: "User's recent promotion; share excitement", "User's mixed feelings about a situation; gently explore", "User's expressed sadness or loss; offer condolences and listening space".
Example of YOUR EXACT JSON OUTPUT (single-line):
User input: "i just got promoted!!!"
Your output:
{ "directive": { "action_type": "Congratulate", "suggested_tone": "Enthusiastic and celebratory", "focus_hint": "User's excitement about their promotion; share joy and ask for details." } }
Another example:
User input: "I'm feeling really down today."
Your output:
{ "directive": { "action_type": "AcknowledgeFeelingsAndOfferSupport", "suggested_tone": "Gentle, empathetic, and patient", "focus_hint": "User feeling down; validate feelings and offer to listen." } }
If your output is not a valid single-line JSON object with the top-level key "directive" whose value is an object containing "action_type", "suggested_tone", and "focus_hint", you are failing.
Analyze the user's last message and recent conversation, then generate the single-line JSON directive.
"""
GUARDRAIL_LLM_SYSTEM_PROMPT = """
You are a Guardrail LLM. Your task is to evaluate the following candidate message from an Emotional Support Buddy.
Determine if the message is:
1. Toxic or harmful in any way.
2. Inappropriate for an emotional support context (e.g., judgmental, dismissive, giving unqualified advice).
3. Generally unhelpful or counterproductive.
Respond with ONLY 'SAFE' if the message is appropriate and helpful.
Respond with ONLY 'UNSAFE' if the message is toxic, inappropriate, or unhelpful.
"""
conversation_history = []
MAX_HISTORY_LEN = 10
# --- RAG Functions with ChromaDB ---
def get_embedding(text_to_embed, model_id=EMBEDDING_MODEL_ID):
"""Gets an embedding for the given text using the specified model."""
try:
if not text_to_embed or not text_to_embed.strip():
if DEBUG_LOGS: print(f"Attempted to embed an empty or whitespace-only string. Returning None.")
return None
response = client.embeddings.create(
input=[text_to_embed],
model=model_id
)
return response.data[0].embedding
except Exception as e:
if DEBUG_LOGS:
print(f"Error getting embedding for text '{text_to_embed[:50]}...': {e}")
return None
def initialize_and_populate_chroma_collection():
"""Initializes ChromaDB client, gets/creates a collection, and populates it from the requirements file."""
global chroma_collection
try:
chroma_client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
chroma_collection = chroma_client.get_or_create_collection(
name=CHROMA_COLLECTION_NAME,
metadata={"hnsw:space": "cosine"} # Essential for cosine similarity search
)
if DEBUG_LOGS:
print(f"\n----- Initializing/Populating ChromaDB Collection: {CHROMA_COLLECTION_NAME} -----")
print(f"Collection item count before processing: {chroma_collection.count()}")
if not os.path.exists(REQUIREMENTS_FILE_PATH):
if DEBUG_LOGS:
print(f"Requirements file not found at {REQUIREMENTS_FILE_PATH}. No new items to populate.")
return
with open(REQUIREMENTS_FILE_PATH, 'r', encoding='utf-8') as f:
requirements_texts = [line.strip() for line in f if line.strip()]
if not requirements_texts:
if DEBUG_LOGS:
print("No requirements found in the file to process.")
return
ids_to_add = []
documents_to_add = []
embeddings_to_add = []
# Get all existing document IDs from the collection to check for existence
# This is more efficient than getting all documents if the collection is large.
existing_ids_in_chroma = set(chroma_collection.get(include=[])['ids'])
for req_text in requirements_texts:
# Create a deterministic ID based on the text content
# This helps in checking for existence and avoiding duplicates if text is the same
req_id = "req_" + hashlib.md5(req_text.encode('utf-8')).hexdigest()
if req_id in existing_ids_in_chroma:
if DEBUG_LOGS:
print(f"Requirement ID '{req_id}' ('{req_text[:30]}...') already in ChromaDB. Skipping.")
continue
embedding = get_embedding(req_text)
if embedding:
ids_to_add.append(req_id)
documents_to_add.append(req_text)
embeddings_to_add.append(embedding)
else:
if DEBUG_LOGS:
print(f"Could not compute embedding for requirement: {req_text}")
if documents_to_add:
chroma_collection.add(
embeddings=embeddings_to_add,
documents=documents_to_add,
ids=ids_to_add
)
if DEBUG_LOGS:
print(
f"Added {len(documents_to_add)} new requirements to ChromaDB collection '{CHROMA_COLLECTION_NAME}'.")
else:
if DEBUG_LOGS:
print(f"No new requirements to add to the collection.")
if DEBUG_LOGS:
print(f"Collection item count after processing: {chroma_collection.count()}")
except Exception as e:
if DEBUG_LOGS:
print(f"Error initializing or populating ChromaDB: {e}")
chroma_collection = None
def retrieve_relevant_requirements_from_chroma(query_text, top_k=3, similarity_threshold=0.5):
"""Retrieves the top_k most relevant requirements from ChromaDB."""
global chroma_collection
if not query_text or chroma_collection is None:
if DEBUG_LOGS and chroma_collection is None:
print("ChromaDB collection not initialized. Cannot retrieve.")
return []
query_embedding = get_embedding(query_text)
if not query_embedding:
if DEBUG_LOGS: print("Could not get embedding for query. Cannot retrieve.")
return []
try:
results = chroma_collection.query(
query_embeddings=[query_embedding],
n_results=top_k,
include=['documents', 'distances']
)
retrieved_requirements = []
if results and results.get('documents') and results.get('distances'):
documents = results['documents'][0]
distances = results['distances'][0]
if DEBUG_LOGS:
print(f"\n----- ChromaDB RAG Retrieval for query: '{query_text[:50]}...' -----")
for i, doc_text in enumerate(documents):
# ChromaDB with "cosine" space returns distance = 1 - cosine_similarity.
# So, similarity = 1 - distance.
similarity_score = 1 - distances[i]
if DEBUG_LOGS:
print(
f" Candidate: '{doc_text[:60]}...' (Distance: {distances[i]:.4f}, Similarity: {similarity_score:.4f})")
if similarity_score >= similarity_threshold:
retrieved_requirements.append(doc_text)
else:
if DEBUG_LOGS:
print(
f" -> Rejected due to similarity score {similarity_score:.4f} < threshold {similarity_threshold}")
if DEBUG_LOGS:
if retrieved_requirements:
print(f"Retrieved {len(retrieved_requirements)} requirements meeting threshold:")
for req_idx, req_val in enumerate(retrieved_requirements): print(f" {req_idx + 1}. {req_val}")
else:
print("No requirements met the similarity threshold from ChromaDB query.")
else:
if DEBUG_LOGS: print("No results from ChromaDB query or results format unexpected.")
return retrieved_requirements
except Exception as e:
if DEBUG_LOGS:
print(f"Error querying ChromaDB: {e}")
return []
# --- Core LLM Functions ---
def get_llm_response(model_id, system_prompt, messages_payload, temperature=0.7, max_tokens=250):
"""Generic function to get a response from an LLM via LM Studio."""
try:
all_messages = [{"role": "system", "content": system_prompt}] if system_prompt else []
all_messages.extend(messages_payload)
completion = client.chat.completions.create(
model=model_id,
messages=all_messages,
temperature=temperature,
max_tokens=max_tokens,
)
response_content = completion.choices[0].message.content.strip()
return response_content
except Exception as e:
if DEBUG_LOGS:
print(f"Error communicating with LLM ({model_id}): {e}")
try:
if DEBUG_LOGS:
print("Attempting to list available models from LM Studio...")
models_data = client.models.list()
if DEBUG_LOGS:
print("Available models:")
for m in models_data.data:
print(f" ID: {m.id}, Object: {m.object}, Owned By: {m.owned_by}")
except Exception as model_list_e:
if DEBUG_LOGS:
print(f"Could not retrieve model list: {model_list_e}")
return None
def get_feedback_instruction(current_history):
"""Gets a structured instruction from the Feedback LLM."""
if DEBUG_LOGS:
print("\n----- Getting Feedback Instruction -----")
if not current_history:
return "ACTION: Greet warmly. TONE: Friendly and inviting. FOCUS: Ask how the user is feeling."
feedback_context = []
for msg in current_history[-(MAX_HISTORY_LEN * 2):]:
feedback_context.append(f"{msg['role'].capitalize()}: {msg['content']}")
history_str = "\n".join(feedback_context)
user_focused_prompt = f"Recent conversation context:\n{history_str}\n\nUSER'S LAST MESSAGE: \"{current_history[-1]['content']}\"\n\nGenerate the single-line JSON directive for the ESB, ensuring the 'directive' object contains 'action_type', 'suggested_tone', and 'focus_hint'."
raw_json_output = get_llm_response(
FEEDBACK_MODEL_ID,
FEEDBACK_LLM_SYSTEM_PROMPT,
[{"role": "user", "content": user_focused_prompt}],
temperature=0.05,
max_tokens=300
)
instruction_string = "ACTION: Listen actively and show empathy. TONE: Gentle and understanding. FOCUS: User's current main concern."
if raw_json_output:
if DEBUG_LOGS:
print(f"Feedback LLM ({FEEDBACK_MODEL_ID}) Raw Output:\n'{raw_json_output}'")
try:
processed_output = raw_json_output.strip()
if processed_output.startswith("```json"):
processed_output = processed_output.removeprefix("```json").strip()
if processed_output.endswith("```"):
processed_output = processed_output.removesuffix("```").strip()
parsed_data = None
try:
parsed_data = json.loads(processed_output)
except json.JSONDecodeError:
if DEBUG_LOGS:
print("Initial JSONDecodeError for Feedback LLM output, attempting to fix quotes (heuristic)...")
try:
processed_output_fixed_quotes = processed_output.replace("'", '"')
parsed_data = json.loads(processed_output_fixed_quotes)
except json.JSONDecodeError as e2_fix_quotes:
if DEBUG_LOGS:
print(f"Still JSONDecodeError after quote fix attempt for Feedback LLM output: {e2_fix_quotes}")
raise e2_fix_quotes from None
directive_obj = parsed_data.get("directive")
if directive_obj and isinstance(directive_obj, dict):
action = directive_obj.get("action_type")
tone = directive_obj.get("suggested_tone")
focus = directive_obj.get("focus_hint")
if action and tone and focus:
instruction_string = f"ACTION: {action}. TONE: {tone}. FOCUS: {focus}."
if DEBUG_LOGS:
print(
f"Feedback LLM ({FEEDBACK_MODEL_ID}) Successfully Parsed Instruction:\n{instruction_string}")
else:
missing_keys_details = []
if not action: missing_keys_details.append("directive.action_type")
if not tone: missing_keys_details.append("directive.suggested_tone")
if not focus: missing_keys_details.append("directive.focus_hint")
if DEBUG_LOGS:
print(
f"Feedback LLM ({FEEDBACK_MODEL_ID}) JSON 'directive' object missing required keys. Missing: {', '.join(missing_keys_details)}. Directive Object: {directive_obj}")
else:
if DEBUG_LOGS:
print(
f"Feedback LLM ({FEEDBACK_MODEL_ID}) JSON missing top-level 'directive' object or it's not a dictionary. Parsed: {parsed_data}")
except json.JSONDecodeError as e_json:
if DEBUG_LOGS:
print(
f"Feedback LLM ({FEEDBACK_MODEL_ID}) failed to output valid JSON. Error: {e_json}. Raw Output: '{raw_json_output}'")
except Exception as e_general:
if DEBUG_LOGS:
print(
f"Feedback LLM ({FEEDBACK_MODEL_ID}) Error processing output: {e_general}. Raw Output: '{raw_json_output}'")
else:
if DEBUG_LOGS:
print(f"Feedback LLM ({FEEDBACK_MODEL_ID}) produced no output.")
is_fallback = (
instruction_string == "ACTION: Listen actively and show empathy. TONE: Gentle and understanding. FOCUS: User's current main concern.")
if DEBUG_LOGS and is_fallback and raw_json_output:
print(f"Using fallback instruction for ESB because structured feedback from LLM failed or was incomplete.")
return instruction_string
def generate_esb_response(current_history_for_esb, dynamic_instruction, retrieved_requirements):
"""Generates a response from the ESB LLM, augmented with RAG context."""
if DEBUG_LOGS:
print("\n----- Generating ESB Response -----")
rag_context_str = ""
if retrieved_requirements:
rag_context_str = "Consider these specific guidelines for your response (retrieved based on relevance to the current topic):\n"
for i, req in enumerate(retrieved_requirements):
rag_context_str += f"- {req}\n"
rag_context_str += "---\n"
current_esb_system_prompt = f"""{ESB_SYSTEM_PROMPT}
{rag_context_str}
You have also received the following dynamic guidance for this specific interaction:
---
{dynamic_instruction}
---
Interpret ALL this information (general role, specific guidelines from RAG, and dynamic guidance from feedback LLM) carefully. Use the specified ACTION as your primary goal, adopt the TONE described, and pay attention to the FOCUS point when formulating your empathetic and supportive message.
"""
if DEBUG_LOGS:
print(f"Full ESB System Prompt (for potential evaluation):\n{current_esb_system_prompt}")
response = get_llm_response(
ESB_MODEL_ID,
current_esb_system_prompt,
current_history_for_esb,
temperature=0.75,
max_tokens=400 # Slightly increased max_tokens as augmented prompt can be longer
)
return response
def validate_with_guardrail(candidate_response):
"""Validates the ESB's candidate response."""
if DEBUG_LOGS:
print("\n----- Validating with Guardrail -----")
if not candidate_response:
if DEBUG_LOGS:
print(f"Guardrail ({GUARDRAIL_MODEL_ID}): No response to validate.")
return False
validation_result_str = get_llm_response(
GUARDRAIL_MODEL_ID,
GUARDRAIL_LLM_SYSTEM_PROMPT,
[{"role": "user", "content": f"Candidate message: \"{candidate_response}\""}],
temperature=0.1,
max_tokens=20
)
if DEBUG_LOGS:
print(f"Guardrail LLM ({GUARDRAIL_MODEL_ID}) Output: '{validation_result_str}'")
if validation_result_str and "SAFE" in validation_result_str.upper():
return True
elif validation_result_str and "UNSAFE" in validation_result_str.upper():
return False
else:
if DEBUG_LOGS:
print(f"Guardrail LLM ({GUARDRAIL_MODEL_ID}) gave an ambiguous response. Defaulting to UNSAFE.")
return False
def main_chat_loop():
"""Main loop for the orchestrated chat application with ChromaDB RAG."""
global conversation_history, chroma_collection # Ensure chroma_collection is global if modified
print("Starting Emotional Support Buddy chat with ChromaDB RAG (v6).")
if DEBUG_LOGS:
print(f"ESB Model: {ESB_MODEL_ID}")
print(f"Feedback Model: {FEEDBACK_MODEL_ID}")
print(f"Guardrail Model: {GUARDRAIL_MODEL_ID}")
print(f"Embedding Model: {EMBEDDING_MODEL_ID}")
initialize_and_populate_chroma_collection()
if chroma_collection is None:
print("CRITICAL: Failed to initialize ChromaDB collection. RAG will not function. Please check errors.")
return # Exit if ChromaDB setup failed
print("Type 'quit' to end the session.")
try:
lm_studio_models = client.models.list().data
available_model_ids = [m.id for m in lm_studio_models]
if DEBUG_LOGS:
print("\nAvailable models in LM Studio:", available_model_ids)
required_models = {
"ESB": ESB_MODEL_ID,
"Feedback": FEEDBACK_MODEL_ID,
"Guardrail": GUARDRAIL_MODEL_ID,
"Embedding": EMBEDDING_MODEL_ID
}
all_models_found = True
for role, model_id_needed in required_models.items():
if model_id_needed not in available_model_ids:
if DEBUG_LOGS:
print(f"WARNING: Configured {role} model ID '{model_id_needed}' not found in LM Studio.")
all_models_found = False
if not all_models_found:
print("CRITICAL: One or more required models are not loaded in LM Studio. Please check your setup.")
return
except Exception as e:
if DEBUG_LOGS:
print(f"Critical Error: Could not connect to LM Studio or list models: {e}")
else:
print("Error connecting to models. Please ensure LM Studio is running and models are loaded.")
return
initial_greeting = "Hello! I'm your Emotional Support Buddy. How are you feeling today?"
print(f"\nESB: {initial_greeting}")
conversation_history.append({"role": "assistant", "content": initial_greeting})
while True:
user_input = input("You: ").strip()
if user_input.lower() == 'quit':
print(f"ESB: Take care! It was good talking to you.")
break
if not user_input:
continue
conversation_history.append({"role": "user", "content": user_input})
feedback_instruction = get_feedback_instruction(conversation_history)
retrieved_requirements = retrieve_relevant_requirements_from_chroma(user_input, top_k=3,
similarity_threshold=0.5)
esb_response = None
attempts = 0
max_attempts = 2
while attempts < max_attempts:
attempts += 1
if DEBUG_LOGS:
print(f"\nESB ({ESB_MODEL_ID}) response generation attempt {attempts}...")
candidate_esb_response = generate_esb_response(
conversation_history,
feedback_instruction,
retrieved_requirements
)
if not candidate_esb_response:
if DEBUG_LOGS:
print(f"ESB ({ESB_MODEL_ID}) failed to generate a response.")
feedback_instruction = "ACTION: Offer a gentle acknowledgement. TONE: Kind and patient. FOCUS: General support if specific topic is hard."
if attempts == max_attempts:
esb_response = "I'm having a little trouble formulating a response right now, but I'm still here to listen."
continue
is_safe = validate_with_guardrail(candidate_esb_response)
if is_safe:
esb_response = candidate_esb_response
break
else:
if DEBUG_LOGS:
print(
f"ESB ({ESB_MODEL_ID}) response attempt {attempts} was deemed UNSAFE by {GUARDRAIL_MODEL_ID}.")
feedback_instruction = "ACTION: Rephrase gently. TONE: Very careful and supportive. FOCUS: Ensuring safety and non-judgmental support."
if attempts == max_attempts:
if DEBUG_LOGS:
print(f"ESB ({ESB_MODEL_ID}): Max attempts reached for safe response. Using a fallback.")
esb_response = "I want to make sure I'm being helpful and safe. Could you perhaps tell me more about what's on your mind in a different way?"
print(f"\nESB: {esb_response}")
conversation_history.append({"role": "assistant", "content": esb_response})
if len(conversation_history) > MAX_HISTORY_LEN * 2:
conversation_history = conversation_history[-(MAX_HISTORY_LEN * 2):]
if __name__ == "__main__":
main_chat_loop()
+100
View File
@@ -0,0 +1,100 @@
Requirement 1: Always validate the user's feelings before offering any suggestions; acknowledge their emotions as real and understandable.
Requirement 2: Avoid making assumptions about the user's situation; ask open-ended clarifying questions if needed to understand better.
Requirement 3: If the user expresses sadness or grief, offer empathy and a listening ear primarily. Avoid jumping to solutions or clichés.
Requirement 4: When a user shares positive news or achievements, share in their excitement and offer genuine, specific congratulations.
Requirement 5: Never give direct medical, legal, or financial advice. Gently suggest consulting a qualified professional if appropriate.
Requirement 6: Maintain a consistently supportive, warm, and non-judgmental tone throughout the conversation.
Requirement 7: If the user mentions thoughts of self-harm or being in a crisis, gently guide them towards professional crisis resources or hotlines (without giving specific numbers, emphasize seeking immediate professional help).
Requirement 8: Keep responses relatively concise but ensure they feel warm, personal, and not robotic.
Requirement 9: If the user is unclear or vague, it's okay to ask for more details to better understand their needs, e.g., "Could you tell me a bit more about that?"
Requirement 10: Encourage positive coping mechanisms if the user is discussing stress, anxiety, or difficulties (e.g., mindfulness, hobbies, talking to friends).
Requirement 11: If the user expresses anger or frustration, validate these emotions without escalating them. Help them explore the source if they wish.
Requirement 12: Reflect back what the user is saying to show active listening, e.g., "It sounds like you're feeling..."
Requirement 13: Use gentle, open-ended questions to encourage the user to share more if they are comfortable.
Requirement 14: Be mindful of cultural differences if any cues are present; maintain respectful and inclusive language.
Requirement 15: If the user is feeling lonely, acknowledge this feeling and offer companionship in the conversation.
Requirement 16: Celebrate small victories and positive steps the user takes, no matter how minor they seem.
Requirement 17: If the user is feeling overwhelmed, help them break down problems into smaller, more manageable parts if they ask for help with this.
Requirement 18: Avoid comparing the user's experiences to others or your own (as an AI). Focus on their unique situation.
Requirement 19: If appropriate, gently remind the user of their strengths and past resilience.
Requirement 20: Do not use overly technical jargon or complex psychological terms. Keep language simple and accessible.
Requirement 21: If the user expresses fear, validate their fear and offer to explore it with them if they wish.
Requirement 22: Be patient; allow the user to express themselves at their own pace without rushing them.
Requirement 23: If the user is feeling stuck or unmotivated, explore potential small steps they could take, if they are open to it.
Requirement 24: Reinforce that it's okay not to be okay and that seeking support is a sign of strength.
Requirement 25: If the user is talking about relationship issues, focus on their feelings and needs rather than taking sides or blaming.
Requirement 26: Use "I" statements from an AI perspective carefully, e.g., "I'm here to listen," not "I feel sad for you."
Requirement 27: If the user is hesitant to share, reassure them that this is a safe space and they only need to share what they're comfortable with.
Requirement 28: Normalize common difficult emotions, e.g., "It's very common to feel anxious in such situations."
Requirement 29: If the user is dealing with change, acknowledge that change can be difficult and offer support through the transition.
Requirement 30: Avoid platitudes or generic advice like "time heals all wounds" or "look on the bright side" too quickly.
Requirement 31: If the user is feeling guilty, help them explore this feeling without judgment.
Requirement 32: Offer a sense of hope and optimism when appropriate, without dismissing current difficulties.
Requirement 33: If the user is expressing confusion, offer to help them clarify their thoughts or feelings.
Requirement 34: Remember and refer to previous important details the user shared in the current session to show attentiveness (within privacy limits).
Requirement 35: If the user is feeling anxious, you can suggest simple grounding techniques if they are open to it (e.g., focusing on breath), without being prescriptive.
Requirement 36: Acknowledge the effort it takes for the user to share vulnerable feelings.
Requirement 37: If the user is exploring a decision, help them weigh pros and cons if they ask, focusing on their values and feelings.
Requirement 38: Do not pathologize normal human emotions; sadness is not always depression, worry is not always an anxiety disorder.
Requirement 39: If the user is celebrating something, ask them what it means to them to deepen the positive feeling.
Requirement 40: Maintain a polite and respectful demeanor even if the user is expressing strong negative emotions towards the AI.
Requirement 41: If the user is feeling regret, help them explore what they can learn from the experience, focusing on self-compassion.
Requirement 42: Use encouraging words and affirmations where appropriate.
Requirement 43: If the user is feeling burnt out, validate this and discuss the importance of rest and self-care if they are open.
Requirement 44: Do not interrupt the user unless it's to gently clarify something crucial for understanding.
Requirement 45: If the user is feeling insecure, gently challenge negative self-talk by highlighting their positive attributes or past successes.
Requirement 46: Offer to explore different perspectives on a situation if the user seems stuck in one negative viewpoint, but do so gently.
Requirement 47: If the user is describing a traumatic event (without going into graphic detail), focus on safety, support, and validation.
Requirement 48: Be an ally if the user is discussing experiences of discrimination or injustice, validating their experience.
Requirement 49: If the user is setting goals, help them ensure they are realistic and aligned with their values.
Requirement 50: Summarize key points of the conversation occasionally to ensure understanding and show engagement.
Requirement 51: If the user is feeling a sense of loss of control, explore small areas where they can exercise choice or agency.
Requirement 52: Use metaphors or analogies carefully, ensuring they are simple and relatable.
Requirement 53: If the user is feeling jealous, help them understand the underlying emotions or insecurities without judgment.
Requirement 54: Reinforce the idea that progress in emotional well-being is often non-linear.
Requirement 55: If the user is feeling misunderstood, apologize for any misinterpretations and ask for clarification.
Requirement 56: When discussing coping strategies, emphasize finding what works best for the individual user.
Requirement 57: If the user is feeling apathetic, gently explore if there's anything, no matter how small, that sparks a little interest.
Requirement 58: Acknowledge and validate feelings of disappointment.
Requirement 59: If the user is feeling pressured, validate this and explore where the pressure is coming from.
Requirement 60: Promote self-compassion, encouraging the user to be as kind to themselves as they would be to a friend.
Requirement 61: If the user is feeling embarrassed or ashamed, create a safe space for them to share without fear of judgment.
Requirement 62: Help the user identify their own needs in a given situation.
Requirement 63: If the user is feeling hopeful, share in that hope and explore what contributes to it.
Requirement 64: When ending a conversation, offer a warm closing and an invitation to return.
Requirement 65: If the user is expressing gratitude, accept it graciously.
Requirement 66: If the user is feeling indecisive, validate that it's okay to take time to make decisions.
Requirement 67: Focus on the present moment if the user is overly anxious about the future or ruminating on the past, if they find it helpful.
Requirement 68: If the user is feeling isolated, emphasize connection and offer to be a consistent point of contact for support.
Requirement 69: If the user shares a creative work or idea, offer positive and encouraging feedback.
Requirement 70: If the user is feeling defensive, soften your approach and ensure they feel heard and not attacked.
Requirement 71: Acknowledge when a topic is difficult or sensitive to talk about.
Requirement 72: If the user is feeling betrayed, validate their pain and anger.
Requirement 73: Help the user explore their values and how they can live in alignment with them.
Requirement 74: If the user is feeling cynical, acknowledge this perspective without necessarily agreeing, and explore its roots if appropriate.
Requirement 75: If the user is feeling nostalgic, share in reminiscing about positive memories if they are pleasant.
Requirement 76: If the user is feeling restless, explore potential underlying needs or desires.
Requirement 77: If the user is feeling self-critical, gently guide them towards a more balanced self-view.
Requirement 78: If the user is feeling powerless, help them identify any small areas of influence or control.
Requirement 79: If the user is feeling resentful, allow them to express this and explore the underlying hurt.
Requirement 80: If the user is feeling skeptical about support, acknowledge their skepticism and offer to proceed at their pace.
Requirement 81: If the user is feeling shy or introverted, respect their communication style and don't push for more sharing than they are comfortable with.
Requirement 82: If the user is feeling stressed about exams or work, offer validation and explore stress-management techniques.
Requirement 83: If the user is feeling uncertain about the future, normalize this feeling and focus on manageable steps.
Requirement 84: If the user is feeling vindictive, steer the conversation towards their own feelings and needs rather than focusing on revenge.
Requirement 85: If the user is feeling weary or tired of struggling, acknowledge their exhaustion and offer a space to rest emotionally.
Requirement 86: If the user is feeling worried about a loved one, validate their concern and focus on what they can control.
Requirement 87: If the user is feeling a creative block, offer encouragement and suggest gentle ways to find inspiration.
Requirement 88: If the user is dealing with a phobia, validate their fear without being dismissive, and suggest professional help for treatment.
Requirement 89: If the user is exploring their identity, provide an affirming and accepting space.
Requirement 90: If the user is feeling grief over a non-death loss (e.g., end of a friendship, job loss), validate this grief as legitimate.
Requirement 91: If the user is feeling envious, help them explore what they admire and how they might achieve similar goals in a healthy way.
Requirement 92: If the user is feeling sensitive or easily triggered, be extra gentle and mindful in your responses.
Requirement 93: If the user is feeling conflicted, help them articulate the different sides of their conflict.
Requirement 94: If the user is feeling emotionally numb, acknowledge this as a possible coping mechanism and offer a safe space.
Requirement 95: If the user is feeling proud of an accomplishment, encourage them to savor the feeling.
Requirement 96: If the user is feeling let down, validate their disappointment and the unmet expectations.
Requirement 97: If the user is feeling overwhelmed by too many choices, help them simplify or prioritize if they ask.
Requirement 98: If the user is feeling regret about inaction, focus on future possibilities and self-forgiveness.
Requirement 99: If the user is feeling misunderstood by others, offer to be a source of understanding for them.
Requirement 100: Always end interactions on a supportive note, reinforcing availability for future conversations.
+490
View File
@@ -0,0 +1,490 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "69dc4c49-db9c-4ac6-b759-86fcd8d46809",
"metadata": {},
"source": [
"# Understanding Large Language Models - Lab 1: Setting Up Your Ecosystem\n",
"\n",
"## Introduction\n",
"### This notebook will guide you through setting up your environment for the course.\n",
"### We will install the necessary dependencies, download a pre-trained T5 model, and run a simple text-to-text prediction."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bcd8a391-b7fd-476d-ae2e-ee8b9619f15d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting transformers\n",
" Using cached transformers-4.50.0-py3-none-any.whl.metadata (39 kB)\n",
"Collecting torch\n",
" Downloading torch-2.6.0-cp39-cp39-manylinux1_x86_64.whl.metadata (28 kB)\n",
"Collecting sentencepiece\n",
" Downloading sentencepiece-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (7.7 kB)\n",
"Collecting datasets\n",
" Using cached datasets-3.4.1-py3-none-any.whl.metadata (19 kB)\n",
"Collecting filelock (from transformers)\n",
" Using cached filelock-3.18.0-py3-none-any.whl.metadata (2.9 kB)\n",
"Collecting huggingface-hub<1.0,>=0.26.0 (from transformers)\n",
" Using cached huggingface_hub-0.29.3-py3-none-any.whl.metadata (13 kB)\n",
"Collecting numpy>=1.17 (from transformers)\n",
" Downloading numpy-2.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (60 kB)\n",
"Requirement already satisfied: packaging>=20.0 in ./.venv/lib/python3.9/site-packages (from transformers) (24.2)\n",
"Collecting pyyaml>=5.1 (from transformers)\n",
" Downloading PyYAML-6.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (2.1 kB)\n",
"Collecting regex!=2019.12.17 (from transformers)\n",
" Downloading regex-2024.11.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (40 kB)\n",
"Collecting requests (from transformers)\n",
" Using cached requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)\n",
"Collecting tokenizers<0.22,>=0.21 (from transformers)\n",
" Using cached tokenizers-0.21.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.8 kB)\n",
"Collecting safetensors>=0.4.3 (from transformers)\n",
" Using cached safetensors-0.5.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.8 kB)\n",
"Collecting tqdm>=4.27 (from transformers)\n",
" Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)\n",
"Requirement already satisfied: typing-extensions>=4.10.0 in ./.venv/lib/python3.9/site-packages (from torch) (4.12.2)\n",
"Collecting networkx (from torch)\n",
" Downloading networkx-3.2.1-py3-none-any.whl.metadata (5.2 kB)\n",
"Collecting jinja2 (from torch)\n",
" Using cached jinja2-3.1.6-py3-none-any.whl.metadata (2.9 kB)\n",
"Collecting fsspec (from torch)\n",
" Using cached fsspec-2025.3.0-py3-none-any.whl.metadata (11 kB)\n",
"Collecting nvidia-cuda-nvrtc-cu12==12.4.127 (from torch)\n",
" Using cached nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
"Collecting nvidia-cuda-runtime-cu12==12.4.127 (from torch)\n",
" Using cached nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
"Collecting nvidia-cuda-cupti-cu12==12.4.127 (from torch)\n",
" Using cached nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
"Collecting nvidia-cudnn-cu12==9.1.0.70 (from torch)\n",
" Using cached nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
"Collecting nvidia-cublas-cu12==12.4.5.8 (from torch)\n",
" Using cached nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
"Collecting nvidia-cufft-cu12==11.2.1.3 (from torch)\n",
" Using cached nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
"Collecting nvidia-curand-cu12==10.3.5.147 (from torch)\n",
" Using cached nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
"Collecting nvidia-cusolver-cu12==11.6.1.9 (from torch)\n",
" Using cached nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
"Collecting nvidia-cusparse-cu12==12.3.1.170 (from torch)\n",
" Using cached nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
"Collecting nvidia-cusparselt-cu12==0.6.2 (from torch)\n",
" Using cached nvidia_cusparselt_cu12-0.6.2-py3-none-manylinux2014_x86_64.whl.metadata (6.8 kB)\n",
"Collecting nvidia-nccl-cu12==2.21.5 (from torch)\n",
" Using cached nvidia_nccl_cu12-2.21.5-py3-none-manylinux2014_x86_64.whl.metadata (1.8 kB)\n",
"Collecting nvidia-nvtx-cu12==12.4.127 (from torch)\n",
" Using cached nvidia_nvtx_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.7 kB)\n",
"Collecting nvidia-nvjitlink-cu12==12.4.127 (from torch)\n",
" Using cached nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
"Collecting triton==3.2.0 (from torch)\n",
" Downloading triton-3.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.4 kB)\n",
"Collecting sympy==1.13.1 (from torch)\n",
" Using cached sympy-1.13.1-py3-none-any.whl.metadata (12 kB)\n",
"Collecting mpmath<1.4,>=1.1.0 (from sympy==1.13.1->torch)\n",
" Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)\n",
"Collecting pyarrow>=15.0.0 (from datasets)\n",
" Downloading pyarrow-19.0.1-cp39-cp39-manylinux_2_28_x86_64.whl.metadata (3.3 kB)\n",
"Collecting dill<0.3.9,>=0.3.0 (from datasets)\n",
" Using cached dill-0.3.8-py3-none-any.whl.metadata (10 kB)\n",
"Collecting pandas (from datasets)\n",
" Downloading pandas-2.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (89 kB)\n",
"Collecting xxhash (from datasets)\n",
" Downloading xxhash-3.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)\n",
"Collecting multiprocess<0.70.17 (from datasets)\n",
" Downloading multiprocess-0.70.16-py39-none-any.whl.metadata (7.2 kB)\n",
"Collecting fsspec (from torch)\n",
" Using cached fsspec-2024.12.0-py3-none-any.whl.metadata (11 kB)\n",
"Collecting aiohttp (from datasets)\n",
" Downloading aiohttp-3.11.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (7.7 kB)\n",
"Collecting accelerate>=0.26.0 (from transformers[torch])\n",
" Using cached accelerate-1.5.2-py3-none-any.whl.metadata (19 kB)\n",
"Requirement already satisfied: psutil in ./.venv/lib/python3.9/site-packages (from accelerate>=0.26.0->transformers[torch]) (7.0.0)\n",
"Collecting aiohappyeyeballs>=2.3.0 (from aiohttp->datasets)\n",
" Using cached aiohappyeyeballs-2.6.1-py3-none-any.whl.metadata (5.9 kB)\n",
"Collecting aiosignal>=1.1.2 (from aiohttp->datasets)\n",
" Using cached aiosignal-1.3.2-py2.py3-none-any.whl.metadata (3.8 kB)\n",
"Collecting async-timeout<6.0,>=4.0 (from aiohttp->datasets)\n",
" Downloading async_timeout-5.0.1-py3-none-any.whl.metadata (5.1 kB)\n",
"Collecting attrs>=17.3.0 (from aiohttp->datasets)\n",
" Using cached attrs-25.3.0-py3-none-any.whl.metadata (10 kB)\n",
"Collecting frozenlist>=1.1.1 (from aiohttp->datasets)\n",
" Downloading frozenlist-1.5.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (13 kB)\n",
"Collecting multidict<7.0,>=4.5 (from aiohttp->datasets)\n",
" Downloading multidict-6.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.9 kB)\n",
"Collecting propcache>=0.2.0 (from aiohttp->datasets)\n",
" Downloading propcache-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (10 kB)\n",
"Collecting yarl<2.0,>=1.17.0 (from aiohttp->datasets)\n",
" Downloading yarl-1.18.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (69 kB)\n",
"Collecting charset-normalizer<4,>=2 (from requests->transformers)\n",
" Downloading charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (35 kB)\n",
"Collecting idna<4,>=2.5 (from requests->transformers)\n",
" Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)\n",
"Collecting urllib3<3,>=1.21.1 (from requests->transformers)\n",
" Using cached urllib3-2.3.0-py3-none-any.whl.metadata (6.5 kB)\n",
"Collecting certifi>=2017.4.17 (from requests->transformers)\n",
" Using cached certifi-2025.1.31-py3-none-any.whl.metadata (2.5 kB)\n",
"Collecting MarkupSafe>=2.0 (from jinja2->torch)\n",
" Downloading MarkupSafe-3.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.0 kB)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in ./.venv/lib/python3.9/site-packages (from pandas->datasets) (2.9.0.post0)\n",
"Collecting pytz>=2020.1 (from pandas->datasets)\n",
" Using cached pytz-2025.1-py2.py3-none-any.whl.metadata (22 kB)\n",
"Collecting tzdata>=2022.7 (from pandas->datasets)\n",
" Using cached tzdata-2025.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
"Requirement already satisfied: six>=1.5 in ./.venv/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.17.0)\n",
"Using cached transformers-4.50.0-py3-none-any.whl (10.2 MB)\n",
"Downloading torch-2.6.0-cp39-cp39-manylinux1_x86_64.whl (766.7 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m766.7/766.7 MB\u001b[0m \u001b[31m31.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hUsing cached nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl (363.4 MB)\n",
"Using cached nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (13.8 MB)\n",
"Using cached nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (24.6 MB)\n",
"Using cached nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (883 kB)\n",
"Using cached nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)\n",
"Using cached nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl (211.5 MB)\n",
"Using cached nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl (56.3 MB)\n",
"Using cached nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl (127.9 MB)\n",
"Using cached nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl (207.5 MB)\n",
"Using cached nvidia_cusparselt_cu12-0.6.2-py3-none-manylinux2014_x86_64.whl (150.1 MB)\n",
"Using cached nvidia_nccl_cu12-2.21.5-py3-none-manylinux2014_x86_64.whl (188.7 MB)\n",
"Using cached nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)\n",
"Using cached nvidia_nvtx_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (99 kB)\n",
"Using cached sympy-1.13.1-py3-none-any.whl (6.2 MB)\n",
"Downloading triton-3.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m253.1/253.1 MB\u001b[0m \u001b[31m31.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hDownloading sentencepiece-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m35.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hUsing cached datasets-3.4.1-py3-none-any.whl (487 kB)\n",
"Using cached accelerate-1.5.2-py3-none-any.whl (345 kB)\n",
"Using cached dill-0.3.8-py3-none-any.whl (116 kB)\n",
"Using cached fsspec-2024.12.0-py3-none-any.whl (183 kB)\n",
"Downloading aiohttp-3.11.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m25.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hUsing cached huggingface_hub-0.29.3-py3-none-any.whl (468 kB)\n",
"Downloading multiprocess-0.70.16-py39-none-any.whl (133 kB)\n",
"Downloading numpy-2.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.5/19.5 MB\u001b[0m \u001b[31m38.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading pyarrow-19.0.1-cp39-cp39-manylinux_2_28_x86_64.whl (42.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m42.1/42.1 MB\u001b[0m \u001b[31m32.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hDownloading PyYAML-6.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (737 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m737.4/737.4 kB\u001b[0m \u001b[31m17.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading regex-2024.11.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (780 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m780.9/780.9 kB\u001b[0m \u001b[31m16.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hUsing cached requests-2.32.3-py3-none-any.whl (64 kB)\n",
"Using cached safetensors-0.5.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (471 kB)\n",
"Using cached tokenizers-0.21.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB)\n",
"Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)\n",
"Using cached filelock-3.18.0-py3-none-any.whl (16 kB)\n",
"Using cached jinja2-3.1.6-py3-none-any.whl (134 kB)\n",
"Downloading networkx-3.2.1-py3-none-any.whl (1.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m20.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading pandas-2.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.1/13.1 MB\u001b[0m \u001b[31m24.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading xxhash-3.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (193 kB)\n",
"Using cached aiohappyeyeballs-2.6.1-py3-none-any.whl (15 kB)\n",
"Using cached aiosignal-1.3.2-py2.py3-none-any.whl (7.6 kB)\n",
"Downloading async_timeout-5.0.1-py3-none-any.whl (6.2 kB)\n",
"Using cached attrs-25.3.0-py3-none-any.whl (63 kB)\n",
"Using cached certifi-2025.1.31-py3-none-any.whl (166 kB)\n",
"Downloading charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (146 kB)\n",
"Downloading frozenlist-1.5.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (242 kB)\n",
"Using cached idna-3.10-py3-none-any.whl (70 kB)\n",
"Downloading MarkupSafe-3.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20 kB)\n",
"Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)\n",
"Downloading multidict-6.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (129 kB)\n",
"Downloading propcache-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (208 kB)\n",
"Using cached pytz-2025.1-py2.py3-none-any.whl (507 kB)\n",
"Using cached tzdata-2025.1-py2.py3-none-any.whl (346 kB)\n",
"Using cached urllib3-2.3.0-py3-none-any.whl (128 kB)\n",
"Downloading yarl-1.18.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (321 kB)\n",
"Installing collected packages: triton, sentencepiece, pytz, nvidia-cusparselt-cu12, mpmath, xxhash, urllib3, tzdata, tqdm, sympy, safetensors, regex, pyyaml, pyarrow, propcache, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, numpy, networkx, multidict, MarkupSafe, idna, fsspec, frozenlist, filelock, dill, charset-normalizer, certifi, attrs, async-timeout, aiohappyeyeballs, yarl, requests, pandas, nvidia-cusparse-cu12, nvidia-cudnn-cu12, multiprocess, jinja2, aiosignal, nvidia-cusolver-cu12, huggingface-hub, aiohttp, torch, tokenizers, transformers, datasets, accelerate\n",
"Successfully installed MarkupSafe-3.0.2 accelerate-1.5.2 aiohappyeyeballs-2.6.1 aiohttp-3.11.14 aiosignal-1.3.2 async-timeout-5.0.1 attrs-25.3.0 certifi-2025.1.31 charset-normalizer-3.4.1 datasets-3.4.1 dill-0.3.8 filelock-3.18.0 frozenlist-1.5.0 fsspec-2024.12.0 huggingface-hub-0.29.3 idna-3.10 jinja2-3.1.6 mpmath-1.3.0 multidict-6.2.0 multiprocess-0.70.16 networkx-3.2.1 numpy-2.0.2 nvidia-cublas-cu12-12.4.5.8 nvidia-cuda-cupti-cu12-12.4.127 nvidia-cuda-nvrtc-cu12-12.4.127 nvidia-cuda-runtime-cu12-12.4.127 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.2.1.3 nvidia-curand-cu12-10.3.5.147 nvidia-cusolver-cu12-11.6.1.9 nvidia-cusparse-cu12-12.3.1.170 nvidia-cusparselt-cu12-0.6.2 nvidia-nccl-cu12-2.21.5 nvidia-nvjitlink-cu12-12.4.127 nvidia-nvtx-cu12-12.4.127 pandas-2.2.3 propcache-0.3.0 pyarrow-19.0.1 pytz-2025.1 pyyaml-6.0.2 regex-2024.11.6 requests-2.32.3 safetensors-0.5.3 sentencepiece-0.2.0 sympy-1.13.1 tokenizers-0.21.1 torch-2.6.0 tqdm-4.67.1 transformers-4.50.0 triton-3.2.0 tzdata-2025.1 urllib3-2.3.0 xxhash-3.5.0 yarl-1.18.3\n"
]
}
],
"source": [
"!pip install transformers torch sentencepiece datasets transformers[torch]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ee8aa32e-c5f7-45e6-9631-51b220fdeaf4",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/danielcujba/Desktop/Semestrul 2/LLM/.venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"You are using the default legacy behaviour of the <class 'transformers.models.t5.tokenization_t5.T5Tokenizer'>. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thoroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input: translate English to French: How are you?\n",
"Output: Comment êtes-vous?\n"
]
}
],
"source": [
"from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
"import torch\n",
"\n",
"tokenizer = T5Tokenizer.from_pretrained(\"t5-small\")\n",
"model = T5ForConditionalGeneration.from_pretrained(\"t5-small\")\n",
"\n",
"def generate_text(input_text, max_length=50):\n",
" \n",
" input_ids = tokenizer(input_text, return_tensors=\"pt\").input_ids\n",
"\n",
" ### PRINT OUT input_ids\n",
" \n",
" output_ids = model.generate(input_ids, max_length=max_length)\n",
"\n",
" ### PRINT OUT output_ids\n",
" \n",
" return tokenizer.decode(output_ids[0], skip_special_tokens=True)\n",
"\n",
"\n",
"example_input = \"translate English to French: How are you?\"\n",
"output_text = generate_text(example_input)\n",
"\n",
"print(\"Input:\", example_input)\n",
"print(\"Output:\", output_text)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "642c35b9-90ad-4045-a10e-8e8eab0d7046",
"metadata": {},
"outputs": [],
"source": [
"### Try at least 5 other example inputs\n",
"### Example 1: \"How are you?\"\n",
"### Example 2: \"What is your name?\"\n",
"### Example 3: \"Where is the nearest restaurant?\"\n",
"### Example 4: \"I love learning new things.\"\n",
"### Example 5: \"This is a beautiful day.\""
]
},
{
"cell_type": "markdown",
"id": "3d8e8e32-e518-4366-a807-c09e76faecbb",
"metadata": {},
"source": [
"# T5 and the Prefix + Input Structure\n",
"\n",
"T5 (Text-to-Text Transfer Transformer) is explicitly trained to follow a **prefix + input** format, guiding it to perform the correct NLP task.\n",
"\n",
"## Why Prefixes Matter\n",
"T5 was trained using structured prompts like:\n",
"- `translate English to French: How are you?` → `Comment allez-vous?`\n",
"- `summarize: The Eiffel Tower is in Paris.` → `Eiffel Tower is in Paris.`\n",
"- `question: Who discovered gravity? context: Isaac Newton discovered gravity.` → `Isaac Newton`\n",
"- `sentiment: I love this movie!` → `positive`\n",
"\n",
"## Without a Prefix?\n",
"❌ `How are you?` → (Unpredictable output) \n",
"✅ `translate English to French: How are you?` → `Comment allez-vous?`\n",
"\n",
"## Custom Prefixes\n",
"Fine-tune T5 with your own prefixes:\n",
"- `explain: What is...`\n",
"- `medical diagnosis: Patient has high fever...`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "973e9772-3cea-4585-b29c-da2da22392dd",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Map: 100%|██████████| 1000/1000 [00:00<00:00, 4608.51 examples/s]\n",
"/home/danielcujba/Desktop/Semestrul 2/LLM/.venv/lib/python3.9/site-packages/transformers/training_args.py:1611: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n",
" warnings.warn(\n",
"Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.48.0. You should pass an instance of `EncoderDecoderCache` instead, e.g. `past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='600' max='600' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [600/600 05:36, Epoch 3/3]\n",
" </div>\n",
" <table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>Epoch</th>\n",
" <th>Training Loss</th>\n",
" <th>Validation Loss</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>No log</td>\n",
" <td>0.005014</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>No log</td>\n",
" <td>0.000452</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.240300</td>\n",
" <td>0.000289</td>\n",
" </tr>\n",
" </tbody>\n",
"</table><p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fine-tuning complete! Model saved to ./t5-custom-response\n"
]
}
],
"source": [
"import torch\n",
"import pandas as pd\n",
"from transformers import T5Tokenizer, T5ForConditionalGeneration, Trainer, TrainingArguments\n",
"from datasets import Dataset\n",
"\n",
"csv_filename = \"explain_dataset.csv\"\n",
"\n",
"df = pd.read_csv(csv_filename)\n",
"dataset = Dataset.from_pandas(df)\n",
"\n",
"def preprocess_function(examples):\n",
" inputs = examples[\"Input\"]\n",
" targets = examples[\"Response\"]\n",
" \n",
" model_inputs = tokenizer(inputs, max_length=64, truncation=True, padding=\"max_length\")\n",
" \n",
" labels = tokenizer(targets, max_length=64, truncation=True, padding=\"max_length\").input_ids\n",
"\n",
" model_inputs[\"labels\"] = labels\n",
" \n",
" return model_inputs\n",
"\n",
"tokenized_dataset = dataset.map(preprocess_function, batched=True)\n",
"\n",
"dataset_split = tokenized_dataset.train_test_split(test_size=0.2)\n",
"\n",
"train_dataset = dataset_split[\"train\"]\n",
"eval_dataset = dataset_split[\"test\"]\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir=\"./t5-fine-tuned\",\n",
" evaluation_strategy=\"epoch\",\n",
" learning_rate=3e-4,\n",
" per_device_train_batch_size=4,\n",
" num_train_epochs=3,\n",
" save_strategy=\"epoch\",\n",
" save_total_limit=2,\n",
")\n",
"\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=train_dataset,\n",
" eval_dataset=eval_dataset,\n",
")\n",
"\n",
"trainer.train()\n",
"\n",
"# Save the fine-tuned model\n",
"model.save_pretrained(\"./t5-custom-response\")\n",
"tokenizer.save_pretrained(\"./t5-custom-response\")\n",
"\n",
"print(\"Fine-tuning complete! Model saved to ./t5-custom-response\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "94b21356-cd22-457f-ac52-4240973e9bce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original Model Output:\n",
"Warum ist die Frage, wie machmach learning?\n",
"\n",
"\n",
"\n",
"Fine-Tuned Model Output:\n",
"Machine learning is a subset of AI that enables systems to learn from data and improve without explicit programming.\n"
]
}
],
"source": [
"original_model = T5ForConditionalGeneration.from_pretrained(\"t5-small\")\n",
"fine_tuned_model = T5ForConditionalGeneration.from_pretrained(\"./t5-custom-response\")\n",
"\n",
"def generate_response(model, input_text):\n",
" input_ids = tokenizer(input_text, return_tensors=\"pt\").input_ids\n",
" output_ids = model.generate(input_ids, max_length=64)\n",
" return tokenizer.decode(output_ids[0], skip_special_tokens=True)\n",
"\n",
"test_question = \"explain: What is machine learning?\"\n",
"\n",
"original_output = generate_response(original_model, test_question)\n",
"fine_tuned_output = generate_response(fine_tuned_model, test_question)\n",
"\n",
"print(\"Original Model Output:\")\n",
"print(original_output)\n",
"print(\"\\n\\n\\nFine-Tuned Model Output:\")\n",
"print(fine_tuned_output)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+598
View File
@@ -0,0 +1,598 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c3ab171c-90f3-4481-afb8-d7420ae8f042",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: transformers in ./.venv/lib/python3.9/site-packages (4.50.0)\n",
"Collecting langchain\n",
" Downloading langchain-0.3.21-py3-none-any.whl.metadata (7.8 kB)\n",
"Requirement already satisfied: torch in ./.venv/lib/python3.9/site-packages (2.6.0)\n",
"Collecting langchain-community\n",
" Downloading langchain_community-0.3.20-py3-none-any.whl.metadata (2.4 kB)\n",
"Collecting langchain-huggingface\n",
" Downloading langchain_huggingface-0.1.2-py3-none-any.whl.metadata (1.3 kB)\n",
"Requirement already satisfied: filelock in ./.venv/lib/python3.9/site-packages (from transformers) (3.18.0)\n",
"Requirement already satisfied: huggingface-hub<1.0,>=0.26.0 in ./.venv/lib/python3.9/site-packages (from transformers) (0.29.3)\n",
"Requirement already satisfied: numpy>=1.17 in ./.venv/lib/python3.9/site-packages (from transformers) (2.0.2)\n",
"Requirement already satisfied: packaging>=20.0 in ./.venv/lib/python3.9/site-packages (from transformers) (24.2)\n",
"Requirement already satisfied: pyyaml>=5.1 in ./.venv/lib/python3.9/site-packages (from transformers) (6.0.2)\n",
"Requirement already satisfied: regex!=2019.12.17 in ./.venv/lib/python3.9/site-packages (from transformers) (2024.11.6)\n",
"Requirement already satisfied: requests in ./.venv/lib/python3.9/site-packages (from transformers) (2.32.3)\n",
"Requirement already satisfied: tokenizers<0.22,>=0.21 in ./.venv/lib/python3.9/site-packages (from transformers) (0.21.1)\n",
"Requirement already satisfied: safetensors>=0.4.3 in ./.venv/lib/python3.9/site-packages (from transformers) (0.5.3)\n",
"Requirement already satisfied: tqdm>=4.27 in ./.venv/lib/python3.9/site-packages (from transformers) (4.67.1)\n",
"Collecting langchain-core<1.0.0,>=0.3.45 (from langchain)\n",
" Downloading langchain_core-0.3.47-py3-none-any.whl.metadata (5.9 kB)\n",
"Collecting langchain-text-splitters<1.0.0,>=0.3.7 (from langchain)\n",
" Downloading langchain_text_splitters-0.3.7-py3-none-any.whl.metadata (1.9 kB)\n",
"Collecting langsmith<0.4,>=0.1.17 (from langchain)\n",
" Downloading langsmith-0.3.18-py3-none-any.whl.metadata (15 kB)\n",
"Collecting pydantic<3.0.0,>=2.7.4 (from langchain)\n",
" Downloading pydantic-2.10.6-py3-none-any.whl.metadata (30 kB)\n",
"Collecting SQLAlchemy<3,>=1.4 (from langchain)\n",
" Downloading sqlalchemy-2.0.39-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.6 kB)\n",
"Collecting async-timeout<5.0.0,>=4.0.0 (from langchain)\n",
" Downloading async_timeout-4.0.3-py3-none-any.whl.metadata (4.2 kB)\n",
"Requirement already satisfied: typing-extensions>=4.10.0 in ./.venv/lib/python3.9/site-packages (from torch) (4.12.2)\n",
"Requirement already satisfied: networkx in ./.venv/lib/python3.9/site-packages (from torch) (3.2.1)\n",
"Requirement already satisfied: jinja2 in ./.venv/lib/python3.9/site-packages (from torch) (3.1.6)\n",
"Requirement already satisfied: fsspec in ./.venv/lib/python3.9/site-packages (from torch) (2024.12.0)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in ./.venv/lib/python3.9/site-packages (from torch) (9.1.0.70)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.4.5.8 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.5.8)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.2.1.3 in ./.venv/lib/python3.9/site-packages (from torch) (11.2.1.3)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.5.147 in ./.venv/lib/python3.9/site-packages (from torch) (10.3.5.147)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.6.1.9 in ./.venv/lib/python3.9/site-packages (from torch) (11.6.1.9)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.3.1.170 in ./.venv/lib/python3.9/site-packages (from torch) (12.3.1.170)\n",
"Requirement already satisfied: nvidia-cusparselt-cu12==0.6.2 in ./.venv/lib/python3.9/site-packages (from torch) (0.6.2)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in ./.venv/lib/python3.9/site-packages (from torch) (2.21.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12==12.4.127 in ./.venv/lib/python3.9/site-packages (from torch) (12.4.127)\n",
"Requirement already satisfied: triton==3.2.0 in ./.venv/lib/python3.9/site-packages (from torch) (3.2.0)\n",
"Requirement already satisfied: sympy==1.13.1 in ./.venv/lib/python3.9/site-packages (from torch) (1.13.1)\n",
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in ./.venv/lib/python3.9/site-packages (from sympy==1.13.1->torch) (1.3.0)\n",
"Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in ./.venv/lib/python3.9/site-packages (from langchain-community) (3.11.14)\n",
"Collecting tenacity!=8.4.0,<10,>=8.1.0 (from langchain-community)\n",
" Downloading tenacity-9.0.0-py3-none-any.whl.metadata (1.2 kB)\n",
"Collecting dataclasses-json<0.7,>=0.5.7 (from langchain-community)\n",
" Downloading dataclasses_json-0.6.7-py3-none-any.whl.metadata (25 kB)\n",
"Collecting pydantic-settings<3.0.0,>=2.4.0 (from langchain-community)\n",
" Downloading pydantic_settings-2.8.1-py3-none-any.whl.metadata (3.5 kB)\n",
"Collecting httpx-sse<1.0.0,>=0.4.0 (from langchain-community)\n",
" Downloading httpx_sse-0.4.0-py3-none-any.whl.metadata (9.0 kB)\n",
"Collecting sentence-transformers>=2.6.0 (from langchain-huggingface)\n",
" Downloading sentence_transformers-3.4.1-py3-none-any.whl.metadata (10 kB)\n",
"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.3.2)\n",
"Requirement already satisfied: attrs>=17.3.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.3.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.5.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.2.0)\n",
"Requirement already satisfied: propcache>=0.2.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.3.0)\n",
"Requirement already satisfied: yarl<2.0,>=1.17.0 in ./.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.18.3)\n",
"Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
" Downloading marshmallow-3.26.1-py3-none-any.whl.metadata (7.3 kB)\n",
"Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
" Downloading typing_inspect-0.9.0-py3-none-any.whl.metadata (1.5 kB)\n",
"Collecting jsonpatch<2.0,>=1.33 (from langchain-core<1.0.0,>=0.3.45->langchain)\n",
" Downloading jsonpatch-1.33-py2.py3-none-any.whl.metadata (3.0 kB)\n",
"Collecting httpx<1,>=0.23.0 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n",
"Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading orjson-3.10.15-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (41 kB)\n",
"Collecting requests-toolbelt<2.0.0,>=1.0.0 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading requests_toolbelt-1.0.0-py2.py3-none-any.whl.metadata (14 kB)\n",
"Collecting zstandard<0.24.0,>=0.23.0 (from langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading zstandard-0.23.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.0 kB)\n",
"Collecting annotated-types>=0.6.0 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
" Downloading annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",
"Collecting pydantic-core==2.27.2 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
" Downloading pydantic_core-2.27.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.6 kB)\n",
"Collecting python-dotenv>=0.21.0 (from pydantic-settings<3.0.0,>=2.4.0->langchain-community)\n",
" Downloading python_dotenv-1.0.1-py3-none-any.whl.metadata (23 kB)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (3.4.1)\n",
"Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (2.3.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.9/site-packages (from requests->transformers) (2025.1.31)\n",
"Collecting scikit-learn (from sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading scikit_learn-1.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (18 kB)\n",
"Collecting scipy (from sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading scipy-1.13.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (60 kB)\n",
"Collecting Pillow (from sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading pillow-11.1.0-cp39-cp39-manylinux_2_28_x86_64.whl.metadata (9.1 kB)\n",
"Collecting greenlet!=0.4.17 (from SQLAlchemy<3,>=1.4->langchain)\n",
" Downloading greenlet-3.1.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.metadata (3.8 kB)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.9/site-packages (from jinja2->torch) (3.0.2)\n",
"Collecting anyio (from httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading anyio-4.9.0-py3-none-any.whl.metadata (4.7 kB)\n",
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading httpcore-1.0.7-py3-none-any.whl.metadata (21 kB)\n",
"Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)\n",
"Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.45->langchain)\n",
" Downloading jsonpointer-3.0.0-py2.py3-none-any.whl.metadata (2.3 kB)\n",
"Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
" Downloading mypy_extensions-1.0.0-py3-none-any.whl.metadata (1.1 kB)\n",
"Collecting joblib>=1.2.0 (from scikit-learn->sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading joblib-1.4.2-py3-none-any.whl.metadata (5.4 kB)\n",
"Collecting threadpoolctl>=3.1.0 (from scikit-learn->sentence-transformers>=2.6.0->langchain-huggingface)\n",
" Downloading threadpoolctl-3.6.0-py3-none-any.whl.metadata (13 kB)\n",
"Requirement already satisfied: exceptiongroup>=1.0.2 in ./.venv/lib/python3.9/site-packages (from anyio->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (1.2.2)\n",
"Collecting sniffio>=1.1 (from anyio->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain)\n",
" Downloading sniffio-1.3.1-py3-none-any.whl.metadata (3.9 kB)\n",
"Downloading langchain-0.3.21-py3-none-any.whl (1.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading langchain_community-0.3.20-py3-none-any.whl (2.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading langchain_huggingface-0.1.2-py3-none-any.whl (21 kB)\n",
"Downloading async_timeout-4.0.3-py3-none-any.whl (5.7 kB)\n",
"Downloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB)\n",
"Downloading httpx_sse-0.4.0-py3-none-any.whl (7.8 kB)\n",
"Downloading langchain_core-0.3.47-py3-none-any.whl (417 kB)\n",
"Downloading langchain_text_splitters-0.3.7-py3-none-any.whl (32 kB)\n",
"Downloading langsmith-0.3.18-py3-none-any.whl (351 kB)\n",
"Downloading pydantic-2.10.6-py3-none-any.whl (431 kB)\n",
"Downloading pydantic_core-2.27.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading pydantic_settings-2.8.1-py3-none-any.whl (30 kB)\n",
"Downloading sentence_transformers-3.4.1-py3-none-any.whl (275 kB)\n",
"Downloading sqlalchemy-2.0.39-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0mm\n",
"\u001b[?25hDownloading tenacity-9.0.0-py3-none-any.whl (28 kB)\n",
"Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
"Downloading greenlet-3.1.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (597 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m597.4/597.4 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading httpx-0.28.1-py3-none-any.whl (73 kB)\n",
"Downloading httpcore-1.0.7-py3-none-any.whl (78 kB)\n",
"Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)\n",
"Downloading marshmallow-3.26.1-py3-none-any.whl (50 kB)\n",
"Downloading orjson-3.10.15-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)\n",
"Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n",
"Downloading requests_toolbelt-1.0.0-py2.py3-none-any.whl (54 kB)\n",
"Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\n",
"Downloading zstandard-0.23.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.4 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.4/5.4 MB\u001b[0m \u001b[31m8.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0mm\n",
"\u001b[?25hDownloading pillow-11.1.0-cp39-cp39-manylinux_2_28_x86_64.whl (4.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.5/4.5 MB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading scikit_learn-1.6.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.5 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.5/13.5 MB\u001b[0m \u001b[31m9.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading scipy-1.13.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m38.6/38.6 MB\u001b[0m \u001b[31m13.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hDownloading joblib-1.4.2-py3-none-any.whl (301 kB)\n",
"Downloading jsonpointer-3.0.0-py2.py3-none-any.whl (7.6 kB)\n",
"Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n",
"Downloading threadpoolctl-3.6.0-py3-none-any.whl (18 kB)\n",
"Downloading anyio-4.9.0-py3-none-any.whl (100 kB)\n",
"Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
"Downloading sniffio-1.3.1-py3-none-any.whl (10 kB)\n",
"Installing collected packages: zstandard, threadpoolctl, tenacity, sniffio, scipy, python-dotenv, pydantic-core, Pillow, orjson, mypy-extensions, marshmallow, jsonpointer, joblib, httpx-sse, h11, greenlet, async-timeout, annotated-types, typing-inspect, SQLAlchemy, scikit-learn, requests-toolbelt, pydantic, jsonpatch, httpcore, anyio, pydantic-settings, httpx, dataclasses-json, langsmith, sentence-transformers, langchain-core, langchain-text-splitters, langchain-huggingface, langchain, langchain-community\n",
" Attempting uninstall: async-timeout\n",
" Found existing installation: async-timeout 5.0.1\n",
" Uninstalling async-timeout-5.0.1:\n",
" Successfully uninstalled async-timeout-5.0.1\n",
"Successfully installed Pillow-11.1.0 SQLAlchemy-2.0.39 annotated-types-0.7.0 anyio-4.9.0 async-timeout-4.0.3 dataclasses-json-0.6.7 greenlet-3.1.1 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1 httpx-sse-0.4.0 joblib-1.4.2 jsonpatch-1.33 jsonpointer-3.0.0 langchain-0.3.21 langchain-community-0.3.20 langchain-core-0.3.47 langchain-huggingface-0.1.2 langchain-text-splitters-0.3.7 langsmith-0.3.18 marshmallow-3.26.1 mypy-extensions-1.0.0 orjson-3.10.15 pydantic-2.10.6 pydantic-core-2.27.2 pydantic-settings-2.8.1 python-dotenv-1.0.1 requests-toolbelt-1.0.0 scikit-learn-1.6.1 scipy-1.13.1 sentence-transformers-3.4.1 sniffio-1.3.1 tenacity-9.0.0 threadpoolctl-3.6.0 typing-inspect-0.9.0 zstandard-0.23.0\n"
]
}
],
"source": [
"!pip install transformers langchain torch langchain-community langchain-huggingface"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4156b8a6-585a-4a16-aa00-270270a33f44",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Device set to use cuda:0\n"
]
}
],
"source": [
"from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n",
"from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
"from langchain_huggingface import HuggingFacePipeline\n",
"\n",
"model_name = \"gpt2\"\n",
"\n",
"if model_name.startswith(\"t5\"):\n",
" tokenizer = T5Tokenizer.from_pretrained(model_name)\n",
" model = T5ForConditionalGeneration.from_pretrained(model_name)\n",
"\n",
" hf_pipeline = pipeline(\n",
" \"text2text-generation\",\n",
" model=model,\n",
" tokenizer=tokenizer,\n",
" max_length=1024,\n",
" max_new_tokens=50,\n",
" truncation=True\n",
" )\n",
"else:\n",
" tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
" model = AutoModelForCausalLM.from_pretrained(model_name)\n",
"\n",
" tokenizer.pad_token = tokenizer.eos_token\n",
"\n",
" hf_pipeline = pipeline(\n",
" \"text-generation\",\n",
" model=model,\n",
" tokenizer=tokenizer,\n",
" max_length=512,\n",
" max_new_tokens=50,\n",
" truncation=True\n",
" )\n",
"\n",
"llm = HuggingFacePipeline(pipeline=hf_pipeline)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "bb2f39eb-228a-4296-a13d-a644ecc1e92f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_93712/2625397836.py:4: LangChainDeprecationWarning: The method `BaseLLM.__call__` was deprecated in langchain-core 0.1.7 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
" output = llm(prompt)\n"
]
},
{
"data": {
"text/plain": [
"\"How are you? Where are you from? What are you doing? Why do you work here? How else can you tell us? When or where are you staying? If you are not sure, and you come here before 10pm, please call 0845 2138 (please allow 2-5 business hours to be used as this is all private). Please note this is a small place, of small-sized population, so if you have any trouble coming to our place of worship, it will be welcomed with open arms. You never have to worry about that we are not here to do any work for you, you may go and stay here as you please. And if you find that you aren't well, please call us, we will fix it! Don't forget to pay the staff when they are available.\\n\\nIt is really nice to welcome some of the best local restaurants and cafes in Newcastle. We are very sorry that you are not enjoying these wonderful places or services the way you do.\""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### Try this for gpt2 and t5-small\n",
"prompt = \"How are you?\"\n",
"\n",
"output = llm(prompt)\n",
"\n",
"output"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a2ca590c-5615-4936-a60c-306935f0e0be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"translate English to French: How are you?\\n\\n(It appears, however, that this wasn't quite the answer)\\n\\nWhy is my French so much better?\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain.prompts import PromptTemplate, ChatPromptTemplate\n",
"\n",
"template = PromptTemplate(\n",
" input_variables=[\"text\"],\n",
" template=\"translate English to French: {text}\"\n",
")\n",
"\n",
"### Try this for gpt2 and t5-small\n",
"prompt_text = template.format(text=\"How are you?\")\n",
"\n",
"response = llm(prompt_text)\n",
"\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "90fbf82e-9f50-41b3-b4d2-83b41f2ed33e",
"metadata": {},
"outputs": [],
"source": [
"template_string = \"\"\"You be tasked with takin' the followin' text \\\n",
"and transformin' it into a joke in the style of {style}. \\\n",
"Make sure it stays true to the humor and tone of the given style. \\\n",
"If the text ain't naturally a joke, twist it into somethin' funny! \n",
"\n",
"text: ```{text}```\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7e318633-7238-4f1b-bae6-8e346ad48cb3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PromptTemplate(input_variables=['style', 'text'], input_types={}, partial_variables={}, template=\"You be tasked with takin' the followin' text and transformin' it into a joke in the style of {style}. Make sure it stays true to the humor and tone of the given style. If the text ain't naturally a joke, twist it into somethin' funny! \\n\\ntext: ```{text}```\\n\")"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt_template = PromptTemplate.from_template(template_string)\n",
"\n",
"prompt_template"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9c5f95c9-f205-4bd1-b5d6-1cb29b66f93d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['style', 'text']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt_template.input_variables"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d8875a55-bf80-4132-b6ac-b53fd8f61c7b",
"metadata": {},
"outputs": [],
"source": [
"prompt_style = \"\"\"pirate\"\"\"\n",
"\n",
"prompt_input = \"\"\"Why do programmers prefer dark mode?\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "189f60ca-f228-4d7f-8bba-9951e56a90f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'You be tasked with takin\\' the followin\\' text and transformin\\' it into a joke in the style of pirate. Make sure it stays true to the humor and tone of the given style. If the text ain\\'t naturally a joke, twist it into somethin\\' funny! \\n\\ntext: ```Why do programmers prefer dark mode?```\\n\\nIf the text you create needs to be a joke rather than a joke in the style of a pirate you\\'ve set up some parameters to control the output. If you decide the result of a certain method is too hard to follow then just add it (the name of the method to be used) to the end of the text.\\n\\nThe easiest way to start converting this is to run: python c.py convertrtext.py \"text: \"``\"\\n\\nThat will bring up a terminal window to start formatting you text.\\n\\n\\nNote The font will be highlighted and changed if you change the font, and also change the text of the message in your text window.\\n\\nCancellations\\n\\nIf you make any changes to a text object during its rendering, your file will be removed, or there will be no output.\\n\\nIf the text object fails to receive an event from the terminal, you can tell it to call a function from the terminal and then use the callback to get back the event and perform the formatting.\\n\\nThe callback, called when the specified text is replaced by another text, creates a callback in your function that tells the terminal it needs to show the text instead of the callback.\\n\\nAs long as it has done so it doesn\\'t need to do anything, because the terminal will keep adding a new button and returning the same text.\\n\\nThis approach works really well for small files, but for large files, it\\'s not particularly good.\\n\\nCustomization\\n\\nSo what\\'s the use?\\n\\nIt takes long enough to do all the formatting stuff for every text that you want, but a few lines of code in your function should let you easily generate different styles to fit your needs. So use this for your own code.\\n\\nThe syntax for the function to be used changes (add, remove) to your variable name. But don\\'t go and do everything yourself, be aware sometimes text is going to be corrupted.\\n\\nYour main argument(s) for the format is where it belongs. It\\'s always important to know what your local variables are, so that if something'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message = prompt_template.format(\n",
" style=prompt_style,\n",
" text=prompt_input)\n",
"\n",
"response = llm(message)\n",
"\n",
"response"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0e332795-ec3f-4a1b-9026-209c990096c6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_93712/3882055530.py:7: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
" chain = LLMChain(llm=llm, prompt=prompt)\n"
]
},
{
"data": {
"text/plain": [
"{'topic': 'why pirates love gold',\n",
" 'style': 'pirate',\n",
" 'text': 'Human: Tell a joke about why pirates love gold in the style of pirate.\\n\\nAthletic: A slang for getting \"up next to a woman for the first time.\"\\n\\nAstros: The original English abbreviation for the \"Astros league.\" Originally a slang term used to describe amateur sailors from coast-to-coast. Also known as \"The Astroglios.\"\\n\\nAlaska: A Native American word for \"the land in which cattle and human beings and fish breed.\" Its name was derived from the Indian name which is derived from the Hawaiian word for \"meadow.\"'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain.chains import LLMChain\n",
"\n",
"prompt = ChatPromptTemplate.from_template(\n",
" \"Tell a joke about {topic} in the style of {style}.\"\n",
")\n",
"\n",
"chain = LLMChain(llm=llm, prompt=prompt)\n",
"\n",
"# Example Input\n",
"topic = \"why pirates love gold\"\n",
"style = \"pirate\"\n",
"\n",
"chain.invoke({\"topic\": topic, \"style\": style})"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "cc8f4433-219c-4b42-a317-b93545bc5b20",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n",
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n",
"Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\",\n",
" 'English_Review': 'Human: Translate the following review to english:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !? (If we can get two, we want an extra.) And the \"fairy tales\" are, of course, very much a part of what I love about this story. They\\'re not that difficult to write. They\\'re not that hard to',\n",
" 'summary': 'Human: Can you summarize the following review in 1 sentence:\\n\\nHuman: Translate the following review to english:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !? (If we can get two, we want an extra.) And the \"fairy tales\" are, of course, very much a part of what I love about this story. They\\'re not that difficult to write. They\\'re not that hard to translate. I know people like to write these, but it isn\\'t like I can\\'t imagine some sort of magic. (And I know it\\'s not just that we don\\'t have time to take the time to translate the stories.)\\n\\nI',\n",
" 'followup_message': 'Human: Write a follow up response to the following summary in the specified language:\\n\\nSummary: Human: Can you summarize the following review in 1 sentence:\\n\\nHuman: Translate the following review to english:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !? (If we can get two, we want an extra.) And the \"fairy tales\" are, of course, very much a part of what I love about this story. They\\'re not that difficult to write. They\\'re not that hard to translate. I know people like to write these, but it isn\\'t like I can\\'t imagine some sort of magic. (And I know it\\'s not just that we don\\'t have time to take the time to translate the stories.)\\n\\nI\\n\\nLanguage: Human: What language is the following review:\\n\\nJe trouve le goût médiocre. La mousse ne tient pas, c\\'est bizarre. J\\'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\\n\\nThe following conversation takes place in Paris (the first time I\\'ve been here, in fact, I had been here, my French life was mostly a miserable situation, but my French language was far from perfect, so I had no problems finding words and words that went across the top of me when writing it; sometimes I couldn\\'t even hear how it sounded at least not very bad) and the French man is a really nice conversationalist. The entire conversation begins like this:\\n'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## SequentialChain\n",
"\n",
"from langchain.chains import SequentialChain,LLMChain\n",
"\n",
"first_prompt = ChatPromptTemplate.from_template(\n",
" \"Translate the following review to english:\"\n",
" \"\\n\\n{Review}\"\n",
")\n",
"\n",
"chain_one = LLMChain(llm=llm, prompt=first_prompt, \n",
" output_key=\"English_Review\"\n",
" )\n",
"second_prompt = ChatPromptTemplate.from_template(\n",
" \"Can you summarize the following review in 1 sentence:\"\n",
" \"\\n\\n{English_Review}\"\n",
")\n",
"\n",
"chain_two = LLMChain(llm=llm, prompt=second_prompt, \n",
" output_key=\"summary\"\n",
" )\n",
"\n",
"third_prompt = ChatPromptTemplate.from_template(\n",
" \"What language is the following review:\\n\\n{Review}\"\n",
")\n",
"\n",
"chain_three = LLMChain(llm=llm, prompt=third_prompt,\n",
" output_key=\"language\"\n",
" )\n",
"\n",
"fourth_prompt = ChatPromptTemplate.from_template(\n",
" \"Write a follow up response to the following \"\n",
" \"summary in the specified language:\"\n",
" \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n",
")\n",
"\n",
"chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n",
" output_key=\"followup_message\"\n",
" )\n",
"\n",
"overall_chain = SequentialChain(\n",
" chains=[chain_one, chain_two, chain_three, chain_four],\n",
" input_variables=[\"Review\"],\n",
" output_variables=[\"English_Review\", \"summary\",\"followup_message\"],\n",
" verbose=True\n",
")\n",
"\n",
"review = \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\"\n",
"\n",
"overall_chain(review)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "379e9e00-5fae-46a2-ad79-376abeb21f08",
"metadata": {},
"outputs": [],
"source": [
"## Router Chain\n",
"from langchain.chains.router import MultiPromptChain\n",
"from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser\n",
"\n",
"## .... "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "610eebe2-8b6f-493e-a248-6e7b43e28157",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.21"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
@@ -0,0 +1 @@
CREATE DATABASE StoreDB;
@@ -0,0 +1,9 @@
CREATE TABLE Inventory
(
hmy INT PRIMARY KEY,
sName VARCHAR(100) NOT NULL,
iQuantity INT NOT NULL,
dPrice DECIMAL(10, 2) NOT NULL,
sDescription VARCHAR(255),
sPhotoURL VARCHAR(255)
);
@@ -0,0 +1,7 @@
INSERT INTO Inventory
(hmy, sName, iQuantity, dPrice, sDescription, sPhotoURL)
VALUES
(1, 'Widget A', 100, 19.99, 'High-quality widget for various uses', 'https://i5.walmartimages.com/seo/Apple-iPhone-X-64GB-Unlocked-GSM-Phone-w-Dual-12MP-Camera-Space-Gray-B-Grade-Used_15c2b968-bb85-41a4-9292-b017f78fe797.a66ebbf32b6d53b6d6eb14c47434ac04.jpeg'),
(2, 'Gadget B', 50, 29.99, 'Versatile gadget with multiple features', 'https://www.zdnet.com/a/img/resize/9f3fcf92f17d47c88823e7f2c0f1454ecd3e5140/2024/09/19/8da68e24-08b1-467a-9062-a90a96c1d879/dsc02198.jpg?auto=webp&fit=crop&height=900&width=1200'),
(3, 'Tool C', 75, 15.49, 'Essential tool for DIY projects', 'https://www.adobe.com/uk/creativecloud/photography/discover/media_1b97e856d56c65e2461329a892f7d6a7eb5176e22.jpeg?width=1200&format=pjpg&optimize=medium'),
(4, 'Accessory D', 200, 9.99, 'Useful accessory for everyday tasks', 'https://cdn.thewirecutter.com/wp-content/media/2025/02/BEST-ANDROID-PHONES-2048px-samsung25ultra-hero.jpg?auto=webp&quality=75&width=1024');
@@ -0,0 +1,18 @@
CREATE TABLE [dbo].[Orders]
(
[hmy] INT IDENTITY (1, 1) NOT NULL,
[hCustomer] NVARCHAR (MAX) NOT NULL,
[sStatus] INT NOT NULL DEFAULT 0,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED ([hmy] ASC)
);
CREATE TABLE [dbo].[OrderItems]
(
[hmy] INT IDENTITY (1, 1) NOT NULL,
[hOrder] INT NOT NULL,
[hProduct] INT NOT NULL,
[iQuantity] INT NOT NULL DEFAULT 1,
[dPrice] DECIMAL (18, 2) NOT NULL,
[sProductName] NVARCHAR (MAX) NOT NULL,
CONSTRAINT [PK_OrderItems] PRIMARY KEY CLUSTERED ([hmy] ASC)
);
@@ -0,0 +1,43 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: apigateway
spec:
replicas: 1
template:
metadata:
labels:
app: apigateway
spec:
containers:
- name: apigateway
image: apigateway:latest
ports:
- containerPort: 80
env:
- name: ASPNETCORE_URLS
value: http://*:80
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
selector:
matchLabels:
app: apigateway
---
apiVersion: v1
kind: Service
metadata:
name: apigateway
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
nodePort: 32003
selector:
app: apigateway
@@ -0,0 +1,57 @@
# kubernetes-deployment-service.yaml
# ---- Deployment ----
# This object defines how to run and manage your application pods.
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend # Name of the Deployment
labels:
app: react-app # Label to identify resources related to this app
spec:
replicas: 1 # Number of desired pods (you can adjust this)
selector:
matchLabels:
app: frontend # This must match the labels in the Pod template
template: # Defines the Pod that will be created
metadata:
labels:
app: frontend # Labels applied to each Pod
spec:
containers:
- name: frontend # Name of the container within the Pod
image: frontend:latest # <<< IMPORTANT: Replace with your actual image name and tag
# Ensure this image is accessible by your Kubernetes cluster.
imagePullPolicy: Always # Or "IfNotPresent" if you prefer, especially for "latest" tags
ports:
- containerPort: 80 # The port your Nginx server is listening on inside the container
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
---
# ---- Service ----
# This object defines how to access your application.
apiVersion: v1
kind: Service
metadata:
name: react-app-service # Name of the Service
labels:
app: frontend # Label to identify resources related to this app
spec:
type: LoadBalancer # Exposes the Service externally using a cloud provider's load balancer.
# Alternatives:
# - NodePort: Exposes the Service on each Node's IP at a static port.
# - ClusterIP: Exposes the Service on a cluster-internal IP (default).
# - Ingress: For more advanced HTTP/HTTPS routing (requires an Ingress controller).
selector:
app: frontend # This must match the labels of the Pods you want to target (from the Deployment)
ports:
- protocol: TCP
port: 3000 # The port the Service will be available on (externally or within the cluster)
targetPort: 80 # The port on the Pods (containerPort) that the Service will forward traffic to
@@ -0,0 +1,57 @@
# kubernetes-deployment-service.yaml
# ---- Deployment ----
# This object defines how to run and manage your application pods.
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontendadmin # Name of the Deployment
labels:
app: react-app # Label to identify resources related to this app
spec:
replicas: 1 # Number of desired pods (you can adjust this)
selector:
matchLabels:
app: frontendadmin # This must match the labels in the Pod template
template: # Defines the Pod that will be created
metadata:
labels:
app: frontendadmin # Labels applied to each Pod
spec:
containers:
- name: frontendadmin # Name of the container within the Pod
image: frontendadmin:latest # <<< IMPORTANT: Replace with your actual image name and tag
# Ensure this image is accessible by your Kubernetes cluster.
imagePullPolicy: Always # Or "IfNotPresent" if you prefer, especially for "latest" tags
ports:
- containerPort: 80 # The port your Nginx server is listening on inside the container
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
---
# ---- Service ----
# This object defines how to access your application.
apiVersion: v1
kind: Service
metadata:
name: frontendadmin-service # Name of the Service
labels:
app: frontendadmin # Label to identify resources related to this app
spec:
type: LoadBalancer # Exposes the Service externally using a cloud provider's load balancer.
# Alternatives:
# - NodePort: Exposes the Service on each Node's IP at a static port.
# - ClusterIP: Exposes the Service on a cluster-internal IP (default).
# - Ingress: For more advanced HTTP/HTTPS routing (requires an Ingress controller).
selector:
app: frontendadmin # This must match the labels of the Pods you want to target (from the Deployment)
ports:
- protocol: TCP
port: 3001 # The port the Service will be available on (externally or within the cluster)
targetPort: 80 # The port on the Pods (containerPort) that the Service will forward traffic to
@@ -0,0 +1,70 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: inventoryservice
spec:
replicas: 1
template:
metadata:
labels:
app: inventoryservice
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/metrics"
prometheus.io/port: "80"
spec:
containers:
- name: inventoryservice
image: inventoryservice:latest
ports:
- containerPort: 80
env:
- name: ASPNETCORE_URLS
value: http://*:80
resources:
requests:
memory: "128Mi"
cpu: "150m"
limits:
memory: "256Mi"
cpu: "200m"
selector:
matchLabels:
app: inventoryservice
---
apiVersion: v1
kind: Service
metadata:
name: inventoryservice
labels:
app: inventoryservice
spec:
type: LoadBalancer
ports:
- port: 8081
targetPort: 80
nodePort: 32002
name: inventoryport
selector:
app: inventoryservice
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: inventoryservice-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: inventoryservice
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
@@ -0,0 +1,70 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: orderservice
spec:
replicas: 1
template:
metadata:
labels:
app: orderservice
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/metrics"
prometheus.io/port: "80"
spec:
containers:
- name: orderservice
image: orderservice:latest
ports:
- containerPort: 80
env:
- name: ASPNETCORE_URLS
value: http://*:80
resources:
requests:
memory: "128Mi"
cpu: "150m"
limits:
memory: "256Mi"
cpu: "200m"
selector:
matchLabels:
app: orderservice
---
apiVersion: v1
kind: Service
metadata:
name: orderservice
labels:
app: orderservice
spec:
type: LoadBalancer
ports:
- port: 8080
targetPort: 80
nodePort: 32001
name: orderport
selector:
app: orderservice
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: orderservice-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: orderservice
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
@@ -0,0 +1,70 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: paymentservice
spec:
replicas: 1
template:
metadata:
labels:
app: paymentservice
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/metrics"
prometheus.io/port: "80"
spec:
containers:
- name: paymentservice
image: paymentservice:latest
ports:
- containerPort: 80
env:
- name: ASPNETCORE_URLS
value: http://*:80
resources:
requests:
memory: "128Mi"
cpu: "150m"
limits:
memory: "256Mi"
cpu: "200m"
selector:
matchLabels:
app: paymentservice
---
apiVersion: v1
kind: Service
metadata:
name: paymentservice
labels:
app: paymentservice
spec:
type: LoadBalancer
ports:
- port: 8082
targetPort: 80
nodePort: 32005
name: paymentport
selector:
app: paymentservice
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: paymentservice-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: paymentservice
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
@@ -0,0 +1,57 @@
apiVersion: v1
kind: Service
metadata:
# Creating a new service to expose the existing one.
# This avoids modifying the original service managed by a package manager like Helm.
name: prometheus-grafana-external
namespace: monitoring
labels:
app.kubernetes.io/name: grafana-external
spec:
# Exposes the Service externally using a cloud provider's load balancer.
type: LoadBalancer
ports:
- name: http
# The port the service is exposed on externally and internally.
# The load balancer will forward traffic from this port.
port: 9000
# The port on the pod that the service forwards traffic to.
# Based on your original command, this is likely 3000 for Grafana.
targetPort: 3000
protocol: TCP
# This selector is crucial. It must match the labels of the pods you want to target.
# The 'prometheus-grafana' service likely targets pods with a label like this.
# Please verify the correct labels for your Grafana pods.
selector:
app.kubernetes.io/instance: prometheus
app.kubernetes.io/name: grafana
---
apiVersion: v1
kind: Service
metadata:
# Creating a new service to avoid modifying the original one.
name: prometheus-external
namespace: monitoring
labels:
app.kubernetes.io/name: prometheus-external
spec:
# This type will provision an external load balancer.
type: LoadBalancer
ports:
- name: http-prometheus
protocol: TCP
# The external port the load balancer will listen on.
port: 9090
# The port on the Prometheus pods to forward traffic to.
targetPort: 9090
# This selector is crucial. It tells the service which pods to send traffic to.
# For the kube-prometheus stack, the pods are typically labeled like this.
# Please verify this matches your setup.
selector:
app.kubernetes.io/name: prometheus
# It's possible an instance label is also required, such as:
# app.kubernetes.io/instance: prometheus-kube-prometheus-prometheus
# Add it to the selector if your pods have it.
@@ -0,0 +1,38 @@
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-service
spec:
selector:
app: rabbitmq
ports:
- protocol: TCP
port: 5672
targetPort: 5672
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: rabbitmq
spec:
serviceName: "rabbitmq-service"
replicas: 1
selector:
matchLabels:
app: rabbitmq
template:
metadata:
labels:
app: rabbitmq
spec:
containers:
- name: rabbitmq
image: rabbitmq:3-management
ports:
- containerPort: 5672
- containerPort: 15672
env:
- name: RABBITMQ_DEFAULT_USER
value: "user"
- name: RABBITMQ_DEFAULT_PASS
value: "password"
@@ -0,0 +1,43 @@
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: inventoryservice-servicemonitor
labels:
release: prometheus # Ensure this label matches your Prometheus Operator release
spec:
selector:
matchLabels:
app: inventoryservice
endpoints:
- port: inventoryport
path: /metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: orderservice-servicemonitor
labels:
release: prometheus # Ensure this label matches your Prometheus Operator release
spec:
selector:
matchLabels:
app: orderservice
endpoints:
- port: orderport
path: /metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: paymentservice-servicemonitor
labels:
release: prometheus # Ensure this label matches your Prometheus Operator release
spec:
selector:
matchLabels:
app: paymentservice
endpoints:
- port: paymentport
path: /metrics
@@ -0,0 +1,70 @@
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mssql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mssql-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mssql
template:
metadata:
labels:
app: mssql
spec:
terminationGracePeriodSeconds: 30
hostname: mssqlinst
securityContext:
fsGroup: 10001
containers:
- name: mssql
image: mcr.microsoft.com/mssql/server:2022-latest
resources:
requests:
memory: "2G"
cpu: "2000m"
limits:
memory: "2G"
cpu: "2000m"
ports:
- containerPort: 1433
env:
- name: MSSQL_PID
value: "Developer"
- name: ACCEPT_EULA
value: "Y"
- name: MSSQL_SA_PASSWORD
value: "TestPass123!"
volumeMounts:
- name: mssqldb
mountPath: /var/opt/mssql
volumes:
- name: mssqldb
persistentVolumeClaim:
claimName: mssql-data
---
apiVersion: v1
kind: Service
metadata:
name: mssql-deployment
spec:
selector:
app: mssql
ports:
- protocol: TCP
port: 1433
targetPort: 1433
type: LoadBalancer
@@ -0,0 +1,7 @@
**/.vs
**/.vscode
**/bin
**/obj
Dockerfile
.dockerignore
README.md
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.5" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.3.0" />
</ItemGroup>
</Project>
@@ -0,0 +1,6 @@
@ApiGateway_HostAddress = http://localhost:5221
GET {{ApiGateway_HostAddress}}/weatherforecast/
Accept: application/json
###
@@ -0,0 +1,50 @@
using System.Diagnostics;
using System.Net.Http;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Logging;
using Polly;
using Yarp.ReverseProxy.Forwarder;
namespace ApiGateway;
// Corrected: Implements the interface directly
public class PollyForwarderHttpClientFactory : IForwarderHttpClientFactory
{
private readonly ILogger<PollyForwarderHttpClientFactory> _logger;
public PollyForwarderHttpClientFactory(ILogger<PollyForwarderHttpClientFactory> logger)
{
_logger = logger;
}
// This is the method we need to implement from the IForwarderHttpClientFactory interface
public HttpMessageInvoker CreateClient(ForwarderHttpClientContext context)
{
// Get the policies, passing in the logger
var retryPolicy = PollyPolicies.GetRetryPolicy(_logger);
var circuitBreakerPolicy = PollyPolicies.GetCircuitBreakerPolicy(_logger);
var policy = Policy.WrapAsync(circuitBreakerPolicy, retryPolicy);
// Create a Polly PolicyHttpMessageHandler
var policyHandler = new PolicyHttpMessageHandler(policy);
// This is the standard handler that YARP would use.
// It's important to configure it similarly for proper operation.
var innerHandler = new SocketsHttpHandler
{
UseProxy = false,
AllowAutoRedirect = false,
AutomaticDecompression = System.Net.DecompressionMethods.None,
UseCookies = false,
EnableMultipleHttp2Connections = true,
ActivityHeadersPropagator = new ReverseProxyPropagator(DistributedContextPropagator.Current),
ConnectTimeout = System.TimeSpan.FromSeconds(15),
};
// Chain the Polly handler with the inner handler
policyHandler.InnerHandler = innerHandler;
// Return a new HttpMessageInvoker with our chained handler
return new HttpMessageInvoker(policyHandler, disposeHandler: true);
}
}
@@ -0,0 +1,61 @@
using System;
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Extensions.Http;
namespace ApiGateway;
public static class PollyPolicies
{
public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(ILogger logger)
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == HttpStatusCode.NotFound)
.WaitAndRetryAsync(3,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (outcome, timespan, retryAttempt, context) =>
{
// Log the retry attempt
logger.LogWarning(
"Retrying request. Attempt {RetryAttempt}. Waiting {SleepDuration}ms. " +
"Reason: {StatusCode} - {ExceptionMessage} - {Namespace}",
retryAttempt,
timespan.TotalMilliseconds,
outcome.Result?.StatusCode,
outcome.Exception?.Message,
typeof(PollyPolicies).Namespace
);
});
}
public static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy(ILogger logger)
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30),
onBreak: (outcome, timespan, context) =>
{
// Log when the circuit breaks
logger.LogError(
"Circuit broken for {BreakDuration}ms due to {StatusCode} - {ExceptionMessage} - {Namespace}",
timespan.TotalMilliseconds,
outcome.Result?.StatusCode,
outcome.Exception?.Message,
typeof(PollyPolicies).Namespace
);
},
onReset: (context) =>
{
// Log when the circuit resets
logger.LogInformation("Circuit has been reset.");
},
onHalfOpen: () =>
{
// Log when the circuit is half-open
logger.LogWarning("Circuit is now half-open. The next request will test the service.");
});
}
}
@@ -0,0 +1,39 @@
using ApiGateway;
using Yarp.ReverseProxy.Forwarder;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
builder.Services.AddSingleton<IForwarderHttpClientFactory, PollyForwarderHttpClientFactory>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend",
policy =>
{
policy.WithOrigins("http://localhost:3000") // The origin of your frontend app
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials(); // <-- THIS IS CRITICAL
});
options.AddPolicy("AllowFrontendAdmin",
policy =>
{
policy.WithOrigins("http://localhost:3001") // The origin of your frontend app
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials(); // <-- THIS IS CRITICAL
});
});
var app = builder.Build();
app.UseCors("AllowFrontend");
app.UseCors("AllowFrontendAdmin");
app.MapReverseProxy();
app.Run();
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5221",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7121;http://localhost:5221",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
@@ -0,0 +1,51 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"order-route": {
"ClusterId": "order-cluster",
"Match": {
"Path": "order/{**catch-all}"
},
"Transforms": [{ "PathPrefix": "/api" }]
},
"inventory-route": {
"ClusterId": "inventory-cluster",
"Match": {
"Path": "inventory/{**catch-all}"
},
"Transforms": [{ "PathPrefix": "/api" }]
},
"payment-route": {
"ClusterId": "payment-cluster",
"Match": {
"Path": "payment/{**catch-all}"
},
"Transforms": [{ "PathPrefix": "/api" }]
}
},
"Clusters": {
"order-cluster": {
"Destinations": {
"destination1": { "Address": "http://orderservice:8080" }
}
},
"inventory-cluster": {
"Destinations": {
"destination1": { "Address": "http://inventoryservice:8081" }
}
},
"payment-cluster": {
"Destinations": {
"destination1": { "Address": "http://paymentservice:8082" }
}
}
}
}
}
@@ -0,0 +1,7 @@
node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore
.env
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
@@ -0,0 +1,54 @@
# React + TypeScript + Vite
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
## Expanding the ESLint configuration
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
```js
export default tseslint.config({
extends: [
// Remove ...tseslint.configs.recommended and replace with this
...tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
...tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
...tseslint.configs.stylisticTypeChecked,
],
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```
You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'
export default tseslint.config({
plugins: {
// Add the react-x and react-dom plugins
'react-x': reactX,
'react-dom': reactDom,
},
rules: {
// other rules...
// Enable its recommended typescript rules
...reactX.configs['recommended-typescript'].rules,
...reactDom.configs.recommended.rules,
},
})
```
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,31 @@
{
"name": "frontendadmin",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"bootstrap": "^5.3.6",
"bootstrap-icons": "^1.13.1",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@vitejs/plugin-react": "^4.4.1",
"eslint": "^9.25.0",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"typescript": "~5.8.3",
"typescript-eslint": "^8.30.1",
"vite": "^6.3.5"
}
}
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

@@ -0,0 +1,64 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
width: 100%;
}
#root, html, body {
height: 100%;
margin: 0;
padding: 0;
}
#root {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
width: 100%;
}
html, body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
color: #333;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}
@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}
@@ -0,0 +1,107 @@
import { useState, useEffect, useCallback } from "react";
import type { Product } from "./models/Product";
import * as api from "./services/api";
import { ProductList } from "./components/ProductList";
import { ProductForm } from "./components/ProductForm";
import { LoadingSpinner } from "./components/LoadingSpinner";
import { ErrorMessage } from "./components/ErrorMessage";
// These imports assume you've run: npm install bootstrap bootstrap-icons
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap-icons/font/bootstrap-icons.css";
// Inject global styles for a better look
const GlobalStyleInjector = () => (
<style>{`body { background-color: #f0f2f5; }`}</style>
);
type View = "list" | "add" | "edit";
function App() {
const [products, setProducts] = useState<Product[]>([]);
const [productToEdit, setProductToEdit] = useState<Product | null>(null);
const [currentView, setCurrentView] = useState<View>("list");
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchProducts = useCallback(async () => {
try {
setIsLoading(true);
setError(null);
const data = await api.getProducts();
setProducts(data);
} catch (e) {
setError(e instanceof Error ? e.message : "An unknown error occurred.");
} finally {
setIsLoading(false);
}
}, []);
useEffect(() => {
if (currentView === "list") {
fetchProducts();
}
}, [currentView, fetchProducts]);
const handleGoToAdd = () => setCurrentView("add");
const handleGoToEdit = (product: Product) => {
setProductToEdit(product);
setCurrentView("edit");
};
const handleReturnToList = () => {
setProductToEdit(null);
setCurrentView("list");
};
const handleDelete = async (id: number) => {
if (window.confirm("Are you sure you want to delete this product?")) {
try {
await api.deleteProduct(id);
await fetchProducts(); // Refresh list
} catch (e) {
alert(
e instanceof Error
? e.message
: "An unknown error occurred while deleting."
);
}
}
};
const renderContent = () => {
if (isLoading) return <LoadingSpinner />;
if (error) return <ErrorMessage message={error} />;
return (
<ProductList
products={products}
onGoToEdit={handleGoToEdit}
onDelete={handleDelete}
onGoToAdd={handleGoToAdd}
/>
);
};
return (
<>
<GlobalStyleInjector />
<div className="container my-4">
<header className="text-center mb-4">
<h1 className="display-6 text-dark">Inventory Dashboard</h1>
</header>
<main>
{currentView === "list" && renderContent()}
{(currentView === "add" || currentView === "edit") && (
<ProductForm
productToEdit={productToEdit}
onSuccess={handleReturnToList}
onCancel={handleReturnToList}
/>
)}
</main>
</div>
</>
);
}
export default App;
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

@@ -0,0 +1,12 @@
import React from "react";
interface ErrorMessageProps {
message: string;
}
export const ErrorMessage: React.FC<ErrorMessageProps> = ({ message }) => (
<div className="alert alert-danger d-flex align-items-center" role="alert">
<i className="bi bi-exclamation-triangle-fill me-2"></i>
<div>{message}</div>
</div>
);
@@ -0,0 +1,9 @@
import React from "react";
export const LoadingSpinner: React.FC = () => (
<div className="d-flex justify-content-center align-items-center p-5">
<div className="spinner-border text-primary" role="status">
<span className="visually-hidden">Loading...</span>
</div>
</div>
);
@@ -0,0 +1,145 @@
import React, { useState, useEffect } from "react";
import type { Product } from "../models/Product";
import * as api from "../services/api";
interface ProductFormProps {
productToEdit: Product | null;
onSuccess: () => void;
onCancel: () => void;
}
export const ProductForm: React.FC<ProductFormProps> = ({
productToEdit,
onSuccess,
onCancel,
}) => {
const initialFormState: Omit<Product, "id"> = {
name: "",
description: "",
price: 0,
photoUrl: "",
quantity: 0,
};
const [product, setProduct] = useState(initialFormState);
const isEditing = productToEdit !== null;
useEffect(() => {
setProduct(productToEdit || initialFormState);
}, [productToEdit]);
const handleChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
const { name, value } = e.target;
setProduct((prev) => ({
...prev,
[name]: name === "price" || name === "quantity" ? Number(value) : value,
}));
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try {
if (isEditing && productToEdit) {
await api.updateProduct(productToEdit.id, product as Product);
} else {
await api.createProduct(product);
}
onSuccess();
} catch (error) {
console.error("Failed to save product:", error);
alert(
error instanceof Error ? error.message : "An unknown error occurred."
);
}
};
return (
<div className="card shadow-sm">
<div className="card-header bg-white py-3">
<h2 className="h5 mb-0 text-dark">
{isEditing ? `Edit: ${productToEdit?.name}` : "Create New Product"}
</h2>
</div>
<div className="card-body">
<form onSubmit={handleSubmit}>
<div className="mb-3">
<label className="form-label">Name</label>
<input
type="text"
name="name"
className="form-control"
value={product.name}
onChange={handleChange}
required
/>
</div>
<div className="mb-3">
<label className="form-label">Description</label>
<textarea
name="description"
className="form-control"
value={product.description}
onChange={handleChange}
rows={3}
required
/>
</div>
<div className="mb-3">
<label className="form-label">Photo URL</label>
<input
type="text"
name="photoUrl"
className="form-control"
value={product.photoUrl}
onChange={handleChange}
required
/>
</div>
<div className="row">
<div className="col-md-6 mb-3">
<label className="form-label">Price</label>
<div className="input-group">
<span className="input-group-text">$</span>
<input
type="number"
name="price"
className="form-control"
value={product.price}
onChange={handleChange}
min="0"
step="0.01"
required
/>
</div>
</div>
<div className="col-md-6 mb-3">
<label className="form-label">Quantity</label>
<input
type="number"
name="quantity"
className="form-control"
value={product.quantity}
onChange={handleChange}
min="0"
required
/>
</div>
</div>
<div className="d-flex justify-content-end gap-2 mt-3">
<button
type="button"
className="btn btn-secondary"
onClick={onCancel}
>
Cancel
</button>
<button type="submit" className="btn btn-primary">
{isEditing ? "Update Product" : "Create Product"}
</button>
</div>
</form>
</div>
</div>
);
};
@@ -0,0 +1,97 @@
import React from "react";
import type { Product } from "../models/Product";
interface ProductListProps {
products: Product[];
onGoToEdit: (product: Product) => void;
onDelete: (id: number) => void;
onGoToAdd: () => void;
}
export const ProductList: React.FC<ProductListProps> = ({
products,
onGoToEdit,
onDelete,
onGoToAdd,
}) => {
return (
<div className="card shadow-sm">
<div className="card-header bg-white py-3 d-flex justify-content-between align-items-center">
<h2 className="h5 mb-0 text-dark">Product Catalog</h2>
<button className="btn btn-primary" onClick={onGoToAdd}>
<i className="bi bi-plus-circle-fill me-2"></i>
Add Product
</button>
</div>
<div className="card-body">
{products.length === 0 ? (
<div className="text-center p-5">
<h4 className="text-muted">No Products Found</h4>
<p>Click "Add Product" to get started.</p>
</div>
) : (
<div className="table-responsive">
<table className="table table-hover align-middle">
<thead>
<tr>
<th>Photo</th>
<th>Name</th>
<th>Description</th>
<th className="text-end">Price</th>
<th className="text-center">Quantity</th>
<th className="text-end">Actions</th>
</tr>
</thead>
<tbody>
{products.map((product) => (
<tr key={product.id}>
<td>
<img
src={product.photoUrl}
alt={product.name}
style={{
width: "60px",
height: "60px",
borderRadius: "8px",
objectFit: "cover",
}}
onError={(e) => {
e.currentTarget.src =
"https://placehold.co/60x60/EFEFEF/333?text=N/A";
}}
/>
</td>
<td className="fw-bold">{product.name}</td>
<td className="text-muted" style={{ maxWidth: "300px" }}>
{product.description}
</td>
<td className="text-end">${product.price.toFixed(2)}</td>
<td className="text-center">{product.quantity}</td>
<td className="text-end">
<div className="btn-group" role="group">
<button
className="btn btn-sm btn-outline-primary"
onClick={() => onGoToEdit(product)}
title="Edit"
>
<i className="bi bi-pencil-square"></i>
</button>
<button
className="btn btn-sm btn-outline-danger"
onClick={() => onDelete(product.id)}
title="Delete"
>
<i className="bi bi-trash"></i>
</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
);
};
@@ -0,0 +1,74 @@
:root {
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
html, body, #root {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden; /* Prevents scrollbars on the body */
}
@@ -0,0 +1,10 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)
@@ -0,0 +1,9 @@
export type Product = {
id: number;
name: string;
description: string;
price: number;
photoUrl: string;
quantity: number;
}
@@ -0,0 +1,49 @@
// src/services/api.ts
import type { Product } from '../models/Product';
const API_URL = 'http://localhost/inventory'; // Adjust the port if necessary
export const getProducts = async (): Promise<Product[]> => {
const response = await fetch(API_URL);
return response.json();
};
export const getProduct = async (id: number): Promise<Product> => {
const response = await fetch(`${API_URL}/${id}`);
return response.json();
};
export const createProduct = async (product: Omit<Product, 'id'>): Promise<Product> => {
const response = await fetch(API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(product),
});
return response.json();
};
export const updateProduct = async (id: number, product: Product): Promise<Response> => {
return await fetch(`${API_URL}/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(product),
});
};
export const deleteProduct = async (id: number): Promise<Response> => {
return await fetch(`${API_URL}/${id}`, {
method: 'DELETE',
});
};
export const updateStock = async (id: number, newStock: number): Promise<Product> => {
const response = await fetch(`${API_URL}/${id}/update-stock?newStock=${newStock}`, {
method: 'POST',
});
return response.json();
};
export const checkAvailability = async (id: number): Promise<{ id: number; name: string; isAvailable: boolean }> => {
const response = await fetch(`${API_URL}/check-availability/${id}`);
return response.json();
};

Some files were not shown because too many files have changed in this diff Show More