Skip to content

Commit 2a3ccb4

Browse files
authored
Ollama Embeddings (#74)
1 parent d2304ea commit 2a3ccb4

File tree

6 files changed

+103
-5
lines changed

6 files changed

+103
-5
lines changed

Diff for: src/Providers/Ollama/Handlers/Embeddings.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EchoLabs\Prism\Providers\Ollama\Handlers;
6+
7+
use EchoLabs\Prism\Embeddings\Request;
8+
use EchoLabs\Prism\Embeddings\Response as EmbeddingsResponse;
9+
use EchoLabs\Prism\Exceptions\PrismException;
10+
use EchoLabs\Prism\ValueObjects\EmbeddingsUsage;
11+
use Illuminate\Http\Client\PendingRequest;
12+
use Illuminate\Http\Client\Response;
13+
use Throwable;
14+
15+
class Embeddings
16+
{
17+
public function __construct(protected PendingRequest $client) {}
18+
19+
public function handle(Request $request): EmbeddingsResponse
20+
{
21+
try {
22+
$response = $this->sendRequest($request);
23+
} catch (Throwable $e) {
24+
throw PrismException::providerRequestError($request->model, $e);
25+
}
26+
27+
$data = $response->json();
28+
29+
if (data_get($data, 'error') || ! $data) {
30+
throw PrismException::providerResponseError(vsprintf(
31+
'OpenAI Error: [%s] %s',
32+
[
33+
data_get($data, 'error.type', 'unknown'),
34+
data_get($data, 'error.message', 'unknown'),
35+
]
36+
));
37+
}
38+
39+
return new EmbeddingsResponse(
40+
embeddings: data_get($data, 'data.0.embedding', []),
41+
usage: new EmbeddingsUsage(data_get($data, 'usage.total_tokens', null)),
42+
);
43+
}
44+
45+
protected function sendRequest(Request $request): Response
46+
{
47+
return $this->client->post(
48+
'embeddings',
49+
[
50+
'model' => $request->model,
51+
'input' => $request->input,
52+
]
53+
);
54+
}
55+
}

Diff for: src/Providers/Ollama/Ollama.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
namespace EchoLabs\Prism\Providers\Ollama;
66

77
use EchoLabs\Prism\Contracts\Provider;
8-
use EchoLabs\Prism\Embeddings\Request as EmbeddingRequest;
9-
use EchoLabs\Prism\Embeddings\Response as EmbeddingResponse;
8+
use EchoLabs\Prism\Embeddings\Request as EmbeddingsRequest;
9+
use EchoLabs\Prism\Embeddings\Response as EmbeddingsResponse;
10+
use EchoLabs\Prism\Providers\Ollama\Handlers\Embeddings;
1011
use EchoLabs\Prism\Providers\Ollama\Handlers\Text;
1112
use EchoLabs\Prism\Providers\ProviderResponse;
1213
use EchoLabs\Prism\Structured\Request as StructuredRequest;
@@ -36,9 +37,14 @@ public function structured(StructuredRequest $request): ProviderResponse
3637
}
3738

3839
#[\Override]
39-
public function embeddings(EmbeddingRequest $request): EmbeddingResponse
40+
public function embeddings(EmbeddingsRequest $request): EmbeddingsResponse
4041
{
41-
throw new \Exception(sprintf('%s does not support embeddings', class_basename($this)));
42+
$handler = new Embeddings($this->client(
43+
$request->clientOptions,
44+
$request->clientRetry
45+
));
46+
47+
return $handler->handle($request);
4248
}
4349

4450
/**

Diff for: tests/Fixtures/FixtureResponse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function filePath(string $filePath): string
2626
return sprintf('%s/%s', __DIR__, $filePath);
2727
}
2828

29-
public static function recordResponses(string $name): void
29+
public static function recordResponses(string $requetPath, string $name): void
3030
{
3131
$iterator = 0;
3232

Diff for: tests/Fixtures/ollama/embeddings-file-1.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"object":"list","data":[{"object":"embedding","embedding":[0.020509344,0.012192823,0.018344216,0.0023875814,-0.027177704,-0.026699923,-0.01865448,-0.011057463,0.0029308598,0.048731625,-0.0042802966,0.017023966,-0.031036498,0.0071946653,-0.01232176,-0.044011135,0.0070678443,-0.027804187,-0.043491956,-0.019183746,-0.034971952,0.053424325,-0.08780117,-0.02713196,-0.029437903,0.014833557,0.02213334,-0.006084592,0.076193444,0.060268402,0.008882311,0.041225728,0.024818106,0.003774014,-0.009611682,-0.020538121,0.019036824,-0.028729724,-0.02132583,-0.0024664241,0.028156577,-0.028399548,0.015633268,-0.056324366,-0.068995714,-0.05100295,0.010358996,-0.023236845,-0.009086173,0.016997077,0.0009530838,0.01440688,0.009283572,-0.020013753,-0.020310158,-0.05663849,-0.012460134,-0.021431128,-0.04030864,0.0467548,0.007877301,0.06773832,-0.021239977,-0.05604481,0.008350607,0.009835493,-0.009438948,0.032770958,0.0062479526,-0.016342273,-0.035680573,0.023196552,-0.003204653,-0.0308936,-0.01310358,-0.029318465,0.042596042,-0.04425907,0.011503847,0.03818353,0.022575047,0.019430215,-0.032436784,0.021304306,-0.015655575,-0.038268983,0.040251266,-0.011370624,-0.0074733645,-0.026807487,0.023139955,0.02066154,-0.0011863485,0.023168212,0.02176119,0.029180758,0.0055770944,0.03079019,-0.0405211,0.015296331,0.0052763997,0.055341527,-0.07365975,0.024282368,-0.019639127,0.036722533,0.012649154,0.042514715,-0.007932737,-0.040765572,-0.0054950197,-0.0033922307,0.004863746,-0.018743295,-0.020027807,0.040178645,-0.000037560465,0.016678613,-0.03141017,-0.02049078,-0.035440758,-0.03807308,-0.047176026,-0.02744291,0.013820444,-0.036454048,0.017041188,0.053192735,-0.07201768,0.014796188,0.012482338,0.015550969,-0.0022258824,0.023932574,-0.013887157,0.034653697,0.009350177,0.02908733,0.025356634,-0.03606721,0.002130375,-0.06144269,0.0033083782,0.08064992,0.015517603,-0.0024480303,-0.00609337,-0.030112533,-0.04682737,0.040077433,-0.0045729643,0.016191356,-0.02313553,0.077322386,0.017285688,-0.030250259,0.0033073432,-0.02858564,-0.011889532,0.005157558,0.0019861546,0.04089193,-0.03462033,0.032601975,-0.023586357,-0.0076375245,-0.029677715,-0.028003395,-0.0292922,-0.036391057,-0.0005842295,0.0011700063,0.035855334,0.02242454,0.035964124,0.036037635,-0.005191178,0.01635707,0.01340469,-0.013398342,-0.01447717,-0.001284939,-0.0055347844,0.020914843,-0.008716281,0.035685536,0.016231572,-0.0031356933,-0.027553523,0.03273822,0.014015928,0.039624013,-0.032107297,0.036266312,-0.0010152147,0.043060284,-0.048620567,-0.0026008275,-0.012444839,-0.047456034,-0.040440764,0.014653465,0.02032786,-0.009891126,-0.0036824476,0.021817729,-0.024290046,0.015812917,-0.06505373,0.020338867,0.05747855,0.032078292,-0.07183755,0.023084773,0.04300592,-0.039235428,-0.071755834,0.005815277,0.029000286,0.03658177,-0.053163275,0.00598681,0.023711024,0.004383274,-0.040955916,-0.012056556,-0.012338925,0.040308807,-0.01742601,0.023622336,0.042908493,-0.012421518,-0.025838856,0.038588513,0.00017401302,0.051064074,0.04596621,0.017297702,0.011005915,-0.010803197,-0.039980646,0.017277118,0.030651622,0.00880659,0.02611485,0.04463101,0.032042827,-0.0151558025,0.013838441,0.009334329,-0.09122934,0.070033535,0.03935235,-0.046968505,-0.043365985,-0.013970756,0.00075088174,0.057451334,0.0042496845,-0.053702984,0.020198153,0.03776839,0.014569647,-0.0062257075,0.008679887,0.022364037,0.0134216,0.0027347703,-0.021086099,-0.023075653,-0.014561604,-0.029040627,-0.09462442,0.04005659,-0.013173263,-0.012607303,-0.010549857,-0.051520694,0.020143572,-0.03760044,-0.0010076764,-0.021306342,-0.05216869,0.020759583,-0.010792517,-0.00074609887,-0.02184436,0.008459449,-0.025512915,0.04799833,-0.025403913,0.03178728,0.022268072,-0.050262194,-0.013195039,-0.018222542,-0.0041963635,0.025147982,-0.023659939,0.0022845564,0.013017302,0.01078089,0.0017417134,-0.016119966,-0.033631396,0.046839625,0.020071162,-0.0036761977,0.016715366,-0.009628044,-0.021183005,0.02243151,-0.021941844,0.011134679,-0.02463911,0.02560426,0.013071366,0.008392724,-0.010537288,-0.024014475,-0.024148198,-0.012549002,0.010227165,0.020715054,-0.0050645624,0.031024745,0.0017097688,-0.0882899,-0.005587447,0.0007693531,-0.0416141,0.01571151,-0.006681159,-0.0003573616,0.0033481168,0.03828058,-0.013146,-0.014669956,-0.013323045,0.0075256946,0.027505072,-0.0081337225,-0.01874238,0.048128597,-0.023318773,0.008435582,0.037548814,-0.044059973,-0.021449363,-0.034161057,0.009678355,0.011261622,0.0097235795,-0.018289113,-0.0039794166,0.029559448,-0.03135313,0.015007534,0.00799995,0.013061065,-0.007858295,0.06557581,0.019637028,-0.009031312,-0.0051825983,-0.009458884,-0.028010465,0.047256116,0.022102328,-0.03545979,0.03956829,0.012820015,-0.004384087,-0.0118199065,0.0029153896,-0.055498403,0.08501991,0.012040126,0.02273305,0.03323883,-0.013479217,0.019923186,0.026971005,0.029252786,-0.009960388,0.016842052,-0.002271614,-0.00576612,0.008240144,-0.029057954,0.003554537,0.015943235,-0.010634214,-0.011122763,-0.052973915,-0.025206653,0.026111621,0.039704338,0.042073067,0.013986945,0.031344224,-0.0060819765,0.029557541,0.0024684563,-0.03695056,0.0065869032,-0.026171818,0.03925401,0.02451841,0.016955726,-0.017290412,0.018682957,-0.029421153,-0.0046676043,0.013774363,0.009955215,-0.02076243,0.0011002293,0.004624502,0.02820639,-0.02801382,-0.0112418635,0.0134827485,0.02140801,0.028760599,-0.005566545,-0.021312045,-0.0451265,0.012807465,0.030365733,-0.023735045,-0.016775457,-0.035122395,-0.031698857,-0.010001053,0.023652555,0.06406543,-0.02134003,-0.003764502,-0.029706318,-0.034295518,0.020217195,0.0011898318,0.010268641,0.055073544,-0.057740748,0.009955218,-0.013120417,0.026827076,-0.042676028,0.009476023,-0.048171926,0.0023767746,-0.019142667,-0.0029655208,-0.039867632,-0.012362362,0.010563641,0.041582685,-0.0034807383,-0.012425191,-0.0016726038,0.00614074,-0.025981262,-0.031943772,0.062302712,-0.0019572878,0.013421759,0.016786441,0.013570521,-0.026991438,0.03158899,-0.029348549,-0.0038743238,-0.019652773,-0.026283337,0.021646727,0.0032571964,-0.03393723,0.019464986,-0.001743996,0.01903832,-0.035555705,-0.037286244,-0.0014975305,-0.022049328,0.023994157,-0.01783259,-0.018787034,0.008696888,-0.009744245,-0.04407966,0.030322459,-0.058100034,0.008550338,-0.032904062,-0.009829568,-0.0054137455,0.01491322,-0.010053039,0.03856763,-0.021036575,-0.047051277,0.043671295,-0.019915372,-0.004284486,-0.01418065,0.006876395,0.011700982,-0.008239241,-0.029183486,0.003296958,-0.021044103,-0.019618232,0.019215647,-0.024509635,-0.009908538,-0.054832023,-0.0072049093,0.06650425,0.03199731,-0.024238229,0.0078109857,0.038945433,-0.014446661,0.017010374,0.013502392,-0.009133898,-0.009848152,-0.030805394,0.055148073,-0.017798739,-0.00621356,-0.04253603,-0.010578547,0.0025188422,-0.004586496,0.046490032,-0.05337808,-0.013141542,-0.03344375,0.025412306,-0.0039971517,0.007491489,-0.055491243,0.032089245,-0.0046784957,0.058281038,-0.0046466216,0.021046557,0.009962189,-0.002717904,0.026516544,0.082603,-0.029077837,0.01290593,0.006391455,-0.01058387,0.040254682,0.024400951,0.031724036,0.013285696,-0.012265994,0.005874969,-0.062834844,-0.011194304,-0.042319067,0.0060894266,0.029267672,-0.026682602,-0.029850308,-0.03149552,0.071781725,0.01686444,-0.0010435245,-0.05305645,0.004619241,-0.027050516,0.016304875,0.007038104,-0.021201257,-0.017926518,-0.004618015,0.017557602,0.01727954,-0.044223286,0.032262832,0.06535496,0.0120296115,0.02067573,-0.081081405,0.016707536,-0.008912979,0.00020318913,-0.011640771,0.00798666,-0.045855552,-0.0009160072,-0.044053778,-0.05873116,-0.04389507,0.04990459,0.0786044,-0.05663821,0.0044793417,-0.0098791355,-0.017335601,-0.037164085,0.0365979,-0.0047151446,0.014558535,0.005788303,0.02509293,0.012348575,0.021205842,-0.03883474,-0.017620869,-0.044916578,0.05419524,0.021841265,0.012489861,0.031568017,0.035238426,-0.018581366,-0.034288876,-0.023188515,0.017061591,-0.028164206,-0.028881494,-0.0028630628,-0.0079988595,-0.046726037,0.0028560522,-0.0063969265,-0.0129284635,-0.019864924,0.014922695,-0.005718685,-0.053424425,0.045383234,0.052135937,0.001246094,-0.0007943881,-0.034018625,-0.011418645,-0.0153584955,0.045801386,0.030296288,-0.0037871015,-0.0053766286,0.030860126,0.0049551963,0.06872493,-0.025638988,-0.030883348,0.043479245,-0.053804163,-0.030996002,-0.026183298,0.035915922,-0.0070655895,0.0010424905,-0.008466688,0.00028415755,0.04622158,0.049525112,0.028752256,-0.005730129,-0.012658529,-0.03924047,-0.030560138,0.0022705377,-0.042220306,-0.011437065,0.004376959,-0.020121956,0.0033965034,0.01305914,0.04140041,-0.08142574,0.009338454,-0.0023026292,0.023993457,-0.031567518,-0.0018641256,0.03280352,-0.047648963,-0.017472344,0.002356902,-0.012406251,-0.011445104,-0.026017172,0.037680496,-0.010556725,-0.031035008,-0.024263477,-0.0018937994,-0.0019042472,-0.03184761,0.0027843087,-0.0049455245,-0.008124385,-0.033282265,0.013125784,0.016495468,0.025253078,-0.023174195,0.006484936,0.05415844,-0.02248724,0.049878377,-0.022059485,0.007258999,-0.030484108,-0.025655404,-0.019121366,0.049670458,-0.028985541,0.041785613,0.022939535,0.018894017,0.05897629,0.0043268506,0.02103977,0.033154868,0.021239165,-0.018814685,0.023349216,-0.00751845,-0.01612768,-0.062952295,0.031066438,0.02712189,0.0064584413,0.0040366226,-0.024037685,0.011104493,-0.04599039,-0.007256441,0.0014207412,0.035844784,0.046921134,-0.04313062,-0.0056215264,0.016108252,0.011538568,0.011047676,-0.04893154,-0.024787059,0.0032278323,-0.055203866,0.039818246,-0.0051867045,-0.033455204,-0.025627485,0.017238734,0.031691723,-0.0007986325,0.031793036,-0.0020650055,-0.016644983,-0.039511092,-0.004421925,0.018349132,-0.010135085,-0.014621701,-0.07977814,0.035400458,0.043930087,-0.0071751294,-0.02610263,-0.018508926,-0.0030393824,-0.052761044,0.04859754,-0.050103907,-0.00633197,-0.054363575,-0.030961143,0.005981407,-0.00044080656,-0.007650516,0.011695983,0.03218925,0.045839537,-0.018783102,0.047873553,0.06879074,-0.016363386,0.025123255,0.011034017,0.029019846,0.017870827,-0.00286183,-0.0025246125,0.02967821,0.082378164,-0.015553099,-0.035023663,0.010661132,0.0048043695,-0.008154165,-0.007063346,0.03796658,0.00076485815,-0.018430224,-0.021304542,-0.05336911,-0.031163262,0.0023702772,0.0020589544,-0.012326234,-0.0014605762,-0.030088343,-0.0143604595,-0.03961806,0.012518321,0.011970211,-0.0058425367,-0.021328341,-0.043691937,0.021130681,0.011895079,0.02698551,0.04416775,0.041582625,0.017038472,0.045462336,-0.016848493,0.004658245,0.028755108,0.04393426,0.015507328,-0.015306311,0.002313575,-0.02062281,-0.015067709,-0.04972711,0.04675397,0.0015541202,-0.033333488,-0.03977873,-0.02657024,-0.018371008,-0.028331442,-0.02741835,-0.023809383,-0.0008749028,0.05218706,-0.029008005,-0.009137109,0.007334674,-0.028372426,0.008021407,0.009583332,0.026405934,-0.013779238,0.08115611,0.014218875,-0.00800871,0.006091713,0.015344242,-0.013087384,0.0048428252,0.048219815,0.016072752,-0.01189035,-0.014786879,-0.061897147,-0.022584058,0.019807411,-0.04837272,0.011321949,-0.028595112,0.022751026,-0.006903802,-0.0019535313,0.015684409,-0.005369777,0.028621234,-0.04552058,0.0018353286,-0.059445877,-0.00037020427,-0.031321723,0.010693053,-0.014128513,-0.038467366,0.017511077,-0.005984351,-0.03728324,-0.054930598,0.022585075,-0.027418729,-0.010688029,0.040488053,0.033764627,0.009402581,-0.02379091,-0.0416868,0.04559521,0.045521375,0.016276596,0.05876588,0.056042496,0.022301508,-0.017228534,-0.02856473,0.016037418,-0.027147442,-0.010560238,-0.040080063,-0.006955227,-0.024322946,-0.04129703,-0.0062322435,0.061939675,0.024569433,-0.0013425661,-0.032848634,-0.001874018,-0.030028204,0.010425302,-0.005748219,0.031049991,0.03194485,-0.021315033,0.01904298,-0.023114335,0.26547107,0.062919594,0.040520858,0.050255515,-0.009531912,0.04940882,0.05943916,0.005247409,0.04803274,-0.027442005,0.040187046,0.0023244456,0.009001815,0.035829846,-0.023159208,0.04126364,-0.030696258,0.0011175087,-0.017106604,-0.017816195,-0.01945008,0.02650344,0.014167762,0.032818086,0.049676195,-0.017559024,-0.0048661334,-0.007980226,0.0203658,-0.035502262,-0.017999562,-0.05492472,-0.0028668225,-0.025573066,-0.025954586,0.040782478,0.023487624,-0.046227988,-0.02569318,0.03044081,-0.016626047,-0.037692502,-0.025475413,-0.016820619,-0.040180735,0.045644455,0.011166822,0.022527883,0.037950747,-0.043664794,0.009674426,-0.03011658,0.021442186,-0.03162207,-0.045302097,-0.031937603,-0.017419992,-0.010300042,-0.012356358,0.060722623,-0.02876097,0.020256793,0.011854121,0.05054614,-0.040259585,0.054195013,0.0035537698,0.036875166,-0.059966292,-0.03202027,-0.004073791,-0.010561003,-0.026824357,-0.055777606,0.031556606,0.032724675,-0.0075916336,0.041614655,-0.00021713889,-0.034160852,0.01839077,-0.0066905515,-0.030906994,0.016928714,0.019194283,0.079581976,-0.037200913,0.033268277,-0.01152672,0.049090806,-0.01521235,0.04566215,0.012382109,0.013773643,0.008639951],"index":0}],"model":"mxbai-embed-large","usage":{"prompt_tokens":512,"total_tokens":512}}

0 commit comments

Comments
 (0)