Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions assets/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ body {

.rag & {
background: #dc8b6e;

a {
color: #f4e973;
}
}

.youtube & {
background: #df3535;

a {
color: #3e2926;
}
}

.wikipedia & {
Expand Down
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"codewithkyrian/chromadb-php": "^0.3.0",
"league/commonmark": "^2.6",
"php-llm/llm-chain": "^0.9.3",
"php-llm/llm-chain-bundle": "dev-feat-prepare-0.8",
"php-llm/llm-chain-bundle": "^0.9",
"phpdocumentor/reflection-docblock": "^5.5",
"phpstan/phpdoc-parser": "^1.33",
"runtime/frankenphp-symfony": "^0.2.0",
Expand Down Expand Up @@ -93,8 +93,6 @@
"App\\Tests\\": "tests/"
}
},
"minimum-stability": "stable",
"prefer-stable": true,
"scripts": {
"post-install-cmd": [
"@auto-scripts"
Expand Down
24 changes: 11 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 30 additions & 5 deletions config/packages/llm_chain.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
llm_chain:
platform:
# anthropic:
# api_key: '%env(ANTHROPIC_API_KEY)%'
# azure:
# gpt_deployment:
# base_url: '%env(AZURE_GPT_BASE_URL)%'
# deployment: '%env(AZURE_GPT_DEPLOYMENT)%'
# api_key: '%env(AZURE_GPT_API_KEY)%'
# api_version: '%env(AZURE_GPT_VERSION)%'
# embeddings_deployment:
# base_url: '%env(AZURE_EMBEDDINGS_BASE_URL)%'
# deployment: '%env(AZURE_EMBEDDINGS_DEPLOYMENT)%'
# api_key: '%env(AZURE_EMBEDDINGS_API_KEY)%'
# api_version: '%env(AZURE_EMBEDDINGS_VERSION)%'
openai:
api_key: '%env(OPENAI_API_KEY)%'
chain:
rag:
# platform: 'llm_chain.platform.anthropic'
model:
name: 'GPT'
version: 'gpt-4o-mini'
Expand All @@ -18,23 +32,34 @@ llm_chain:
model:
name: 'GPT'
version: 'gpt-4o-mini'
options:
temperature: 0.5
tools:
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia'
store:
chroma_db:
symfonycon:
host: '%env(CHROMADB_HOST)%'
collection: 'symfony_blog'
# web_summer_camp:
# host: '%env(CHROMADB_HOST)%'
# collection: 'wsc_program'
embedder:
default:
# platform: 'llm_chain.platform.anthropic'
# store: 'llm_chain.store.chroma_db.symfonycon'
model:
name: 'Embeddings'
version: 'text-embedding-ada-002'

services:
_defaults:
autowire: true
autoconfigure: true

# PhpLlm\LlmChain\Chain\ToolBox\Tool\Clock: ~
# PhpLlm\LlmChain\Chain\ToolBox\Tool\OpenMeteo: ~
# PhpLlm\LlmChain\Chain\ToolBox\Tool\SerpApi:
# $apiKey: '%env(SERP_API_KEY)%'
PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia: ~
PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch: ~

# TODO: move to configuration
PhpLlm\LlmChain\Bridge\OpenAI\Embeddings: ~
PhpLlm\LlmChain\Model\EmbeddingsModel: '@PhpLlm\LlmChain\Bridge\OpenAI\Embeddings'

50 changes: 9 additions & 41 deletions src/Blog/Embedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,25 @@

namespace App\Blog;

use Codewithkyrian\ChromaDB\Client;
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Document\Vector;
use PhpLlm\LlmChain\Model\Response\AsyncResponse;
use PhpLlm\LlmChain\Model\Response\VectorResponse;
use PhpLlm\LlmChain\PlatformInterface;
use PhpLlm\LlmChain\Document\Metadata;
use PhpLlm\LlmChain\Document\TextDocument;
use PhpLlm\LlmChain\Embedder as LlmChainEmbedder;

final readonly class Embedder
{
public function __construct(
private Loader $loader,
private PlatformInterface $platform,
private Client $chromaClient,
private LlmChainEmbedder $embedder,
) {
}

public function embedBlog(): void
{
$posts = $this->loader->load();
$vectors = $this->createEmbeddings($posts);
$this->pushToChromaDB($posts, $vectors);
}

/**
* @param Post[] $posts
*
* @return Vector[]
*/
private function createEmbeddings(array $posts): array
{
$texts = array_map(fn (Post $post) => $post->toString(), $posts);
$response = $this->platform->request(new Embeddings(), $texts);

assert($response instanceof AsyncResponse);
$response = $response->unwrap();
assert($response instanceof VectorResponse);

return $response->getContent();
}

/**
* @param Post[] $posts
* @param Vector[] $vectors
*/
private function pushToChromaDB(array $posts, array $vectors): void
{
$collection = $this->chromaClient->getOrCreateCollection('symfony_blog');

$ids = array_map(fn (Post $post) => $post->id, $posts);
$vectors = array_map(fn (Vector $vector) => $vector->getData(), $vectors);
$documents = [];
foreach ($this->loader->load() as $post) {
$documents[] = new TextDocument($post->id, $post->toString(), new Metadata($post->toArray()));
}

$collection->upsert($ids, $vectors, $posts);
$this->embedder->embed($documents);
}
}
101 changes: 0 additions & 101 deletions tests/Blog/EmbedderTest.php

This file was deleted.

Loading