Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlive committed Oct 7, 2024
1 parent e7523ce commit 7b145d8
Show file tree
Hide file tree
Showing 91 changed files with 3,432 additions and 165 deletions.
53 changes: 53 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
world that developers are civilized and selfless people.

It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.

## Viability

When requesting or submitting new features, first consider whether it might be useful to others. Open
source projects are used by many developers, who may have entirely different needs to your own. Think about
whether or not your feature is likely to be used by other users of the project.

## Procedure

Before filing an issue:

- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
- Check to make sure your feature suggestion isn't already present within the project.
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
- Check the pull requests tab to ensure that the feature isn't already in progress.

Before submitting a pull request:

- Check the codebase to ensure that your feature doesn't already exist.
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **Pint Coding Standard** - The easiest way to apply the conventions is to install [Laravel Pint](https://laravel.com/docs/11.x/pint).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [](http://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
6 changes: 6 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Reporting Security Issues
If you believe you have found a security vulnerability in Prism, we encourage you to let us know right away.

We will investigate all legitimate reports and do our best to quickly fix the problem.

Email `security@echolabs.dev` to disclose any security vulnerabilities.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ composer.lock
/build/
task/
.idea/
scratch
phpunit.xml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 TJ MIller

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,51 @@

# Prism

Prism makes it easier to work with LLMs by offering a consistent integration
method for your app. This way, you can focus on developing outstanding AI
Prism is a powerful Laravel package for integrating Large Language Models (LLMs) into your applications. It provides a fluent interface for generating text, handling multi-step conversations, and utilizing tools with various AI providers. This way, you can focus on developing outstanding AI
applications for your users without getting lost in the technical intricacies.

## Generating Text
Official documentation can be found at [prism.echolabs.dev](https://prism.echolabs.dev).

## Installation

### Step 1: Composer Installation

First, let's add Prism to your project using Composer. Open your terminal, navigate to your project directory, and run:

```shell
composer require echolabsdev/prism
```

This command will download Prism and its dependencies into your project.

### Step 2: Publish the Configuration

Prism comes with a configuration file that you'll want to customize. Publish it to your config directory by running:

```shell
php artisan vendor:publish --tag=prism-config
```

This will create a new file at `config/prism.php`. We'll explore how to configure Prism in the next section.

## Usage

```php
<?php

$response = Prism::using('anthropic', 'claude-3-5-sonnet-20240620')
->generateText()
->withSystemMessage(view('prompts.nyx'))
->withPrompt('Explain quantum computing to a 5-year-old.')();

echo $response->text;
```

## Authors

This library is created by [TJ Miller](https://tjmiller.me) with contributions from the [Open Source Community](https://github.com/vercel/ai/graphs/contributors).

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.

9 changes: 5 additions & 4 deletions bin/git-hooks/formatting
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env bash

_print() {
_header() {
printf "\e[38;5;4m%s\e[0m\n" "$1"
}

main() {
files_before_format=$(git diff --name-only --diff-filter=d)

_print "Running pint..."
_header "Running pint..."
vendor/bin/pint --dirty

_print "Running rector..."
_header "Running rector..."
vendor/bin/rector process --no-diffs

files_after_format=$(git diff --name-only --diff-filter=d)
Expand All @@ -19,7 +19,8 @@ main() {
files_fixed_by_format=$(comm -13 <(sort <<<"$files_before_format") <(sort <<<"$files_after_format"))

# Re-stage files fixed by pint
_print "Re-staging changed files..."
_header "Re-staging changed files..."
for f in $files_fixed_by_format; do echo "Re-staging: $f"; done || exit
for f in $files_fixed_by_format; do git add "$f"; done || exit
}

Expand Down
51 changes: 44 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "echolabs/prism",
"description": "AI",
"name": "echolabsdev/prism",
"description": "A powerful Laravel package for integrating Large Language Models (LLMs) into your applications.",
"type": "library",
"license": "MIT",
"autoload": {
Expand All @@ -15,6 +15,7 @@
}
],
"require": {
"php": "^8.3",
"laravel/framework": "^11.0"
},
"config": {
Expand All @@ -25,22 +26,33 @@
}
},
"require-dev": {
"pestphp/pest": "^2.34",
"pestphp/pest": "^3.0",
"laravel/pint": "^1.14",
"phpstan/phpstan": "^1.11",
"pestphp/pest-plugin-arch": "^2.7",
"pestphp/pest-plugin-laravel": "^2.4",
"pestphp/pest-plugin-arch": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.2",
"rector/rector": "^1.1",
"projektgopher/whisky": "^0.7.0"
"projektgopher/whisky": "^0.7.0",
"orchestra/testbench": "^9.4",
"mockery/mockery": "^1.6"
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"scripts": {
"post-install-cmd": [
"whisky update"
],
"post-update-cmd": [
"whisky update"
],
"post-autoload-dump": [
"@clear",
"@prepare"
Expand All @@ -54,6 +66,31 @@
],
"test": [
"@php vendor/bin/pest"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve --ansi"
],
"lint": [
"@php vendor/bin/pint --ansi",
"@php vendor/bin/phpstan analyse --verbose --ansi"
],
"toc": [
"markdown-toc -i README.md"
]
},
"extra": {
"laravel": {
"providers": [
"EchoLabs\\Prism\\PrismServiceProvider"
],
"aliases": {
"PrismServer": "EchoLabs\\Prism\\Facades\\PrismServer"
}
}
}
}
7 changes: 7 additions & 0 deletions config/prism.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

return [
'prism_server' => [
'enabled' => env('PRISM_SERVER_ENABLED', true),
],
'providers' => [
'openai' => [
'driver' => 'openai',
Expand All @@ -12,5 +15,9 @@
'api_key' => env('ANTHROPIC_API_KEY'),
'version' => env('ANTHROPIC_API_VERSION', '2023-06-01'),
],
'ollama' => [
'driver' => 'openai',
'url' => env('OLLAMA_URL', 'http://localhost:11434/v1'),
],
],
];
Binary file added docs/images/prism-sever-open-webui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 0 additions & 18 deletions phpunit.xml

This file was deleted.

4 changes: 2 additions & 2 deletions ray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
* This settings controls whether data should be sent to Ray.
*/
'enable' => $_ENV['RAY_ENABLED'] ?? false,
'enable' => true,

/*
* The host used to communicate with the Ray app.
Expand All @@ -16,7 +16,7 @@
*/
// 'port' => 23517,
// Buggerator config
'port' => 8000,
'port' => 8001,

/*
* Absolute base path for your sites or projects in Homestead, Vagrant, Docker, or another remote development server.
Expand Down
35 changes: 35 additions & 0 deletions src/Concerns/HandlesToolCalls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace EchoLabs\Prism\Concerns;

use EchoLabs\Prism\Exceptions\PrismException;
use EchoLabs\Prism\Tool;
use EchoLabs\Prism\ValueObjects\ToolCall;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\Support\MultipleItemsFoundException;
use Throwable;

trait HandlesToolCalls
{
/**
* @param array<int, Tool> $tools
*/
protected function handleToolCall(array $tools, ToolCall $toolCall): string
{
try {
/** @var Tool $tool */
$tool = collect($tools)
->sole(fn (Tool $tool): bool => $tool->name() === $toolCall->name);

return call_user_func_array($tool->handle(...), $toolCall->arguments());
} catch (ItemNotFoundException $e) {
throw PrismException::toolNotFound($toolCall, $e);
} catch (MultipleItemsFoundException $e) {
throw PrismException::multipleToolsFound($toolCall, $e);
} catch (Throwable $e) {
throw PrismException::toolCallFailed($toolCall, $e);
}
}
}
20 changes: 20 additions & 0 deletions src/Concerns/HasDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace EchoLabs\Prism\Concerns;

use EchoLabs\Prism\Contracts\Driver;

trait HasDriver
{
protected Driver $driver;

public function using(string $provider, string $model): self
{
$this->driver = app('prism-manager')->resolve($provider);
$this->driver->usingModel($model);

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Concerns/HasModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace EchoLabs\Prism\Concerns;

trait HasModel
{
protected string $model;
}
Loading

0 comments on commit 7b145d8

Please sign in to comment.