Skip to content

Commit

Permalink
Merge pull request #4 from use-the-fork/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
use-the-fork authored Oct 22, 2024
2 parents 9c28442 + bf378c0 commit b57c957
Show file tree
Hide file tree
Showing 118 changed files with 2,842 additions and 428 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main
- dev-main
- development
jobs:
test:
runs-on: ubuntu-22.04
Expand All @@ -24,6 +24,7 @@ jobs:
CRUNCHBASE_API_KEY: FOO
FIRECRAWL_API_KEY: FOO
SERPAPI_API_KEY: FOO
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

steps:
- name: Checkout 🛎
Expand Down
5 changes: 5 additions & 0 deletions config/synapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
'key' => env('ANTHROPIC_API_KEY'),
'chat_model' => env('ANTHROPIC_API_CHAT_MODEL', 'claude-3-5-sonnet-20240620'),
],
'ollama' => [
'base_url' => env('OLLAMA_BASE_URL'),
'chat_model' => env('OLLAMA_API_CHAT_MODEL', 'llama3.2'),
'embedding_model' => env('OLLAMA_API_CHAT_MODEL', 'llama3.2'),
],
],
'services' => [
'serp_api' => [
Expand Down
55 changes: 32 additions & 23 deletions docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defineConfig} from 'vitepress';
import { defineConfig } from 'vitepress';
// https://vitepress.dev/reference/site-config
export default defineConfig({
title: '🧠 Synapse',
Expand All @@ -9,6 +9,15 @@ export default defineConfig({
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Home', link: '/' },
{
text: '0.2.0',
items: [
{
text: 'Changelog',
link: 'https://github.com/use-the-fork/synapse/tree/v0.2.0',
},
],
},
{
text: '0.1.0',
items: [
Expand All @@ -31,28 +40,28 @@ export default defineConfig({
},
],
},
{
text: 'Prebuilt Agents',
items: [
{ text: 'Introduction', link: '/prebuilt-agents/' },
{
text: 'Multi Query Retriever',
link: '/prebuilt-agents/multi-query-retriever-agent',
},
{
text: 'Contextual Retrieval Preprocessing',
link: '/prebuilt-agents/contextual-retrieval-preprocessing-agent',
},
{
text: 'Chat Rephrase',
link: '/prebuilt-agents/chat-rephrase-agent',
},
{
text: 'SQL Tool',
link: '/prebuilt-agents/sql-tool-agent',
},
],
},
{
text: 'Prebuilt Agents',
items: [
{ text: 'Introduction', link: '/prebuilt-agents/' },
{
text: 'Multi Query Retriever',
link: '/prebuilt-agents/multi-query-retriever-agent',
},
{
text: 'Contextual Retrieval Preprocessing',
link: '/prebuilt-agents/contextual-retrieval-preprocessing-agent',
},
{
text: 'Chat Rephrase',
link: '/prebuilt-agents/chat-rephrase-agent',
},
{
text: 'SQL Tool',
link: '/prebuilt-agents/sql-tool-agent',
},
],
},
{
text: 'Agents',
items: [
Expand Down
18 changes: 16 additions & 2 deletions docs/agents/integrations.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Integrations

Synapse currently supports two integrations: OpenAI and Claude. To use either, add the relevant API key to your `.env` file.
Synapse currently supports three integrations: OpenAI, Claude, and Ollama. To use them, add the relevant API key to your `.env` file.

```dotenv
OLLAMA_BASE_URL=https://foo.bar:1234
OPENAI_API_KEY=
ANTHROPIC_API_KEY=
```

Then, implement the chosen integration in your agent's `resolveIntegration` method.
Then, either implement the chosen integration in your agent's `resolveIntegration` method or set the default integration in your synapse config.

## OpenAI

Expand All @@ -34,3 +35,16 @@ public function resolveIntegration(): Integration
return new ClaudeIntegration;
}
```

## Ollama

```php
use UseTheFork\Synapse\Integrations\OllamaIntegration;

...

public function resolveIntegration(): Integration
{
return new OllamaIntegration;
}
```
9 changes: 9 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ OPENAI_API_EMBEDDING_MODEL=text-embedding-ada-002
ANTHROPIC_API_KEY=
ANTHROPIC_API_CHAT_MODEL=claude-3-5-sonnet-20240620
OLLAMA_BASE_URL=https://foo.bar:1234
OLLAMA_API_CHAT_MODEL=llama3.2
OLLAMA_API_EMBEDDING_MODEL=llama3.2
SERPAPI_API_KEY=
SERPER_API_KEY=
CLEARBIT_API_KEY=
Expand Down Expand Up @@ -80,6 +84,11 @@ Open the `config/synapse.php` file and modify the default integration to the one
'key' => env('ANTHROPIC_API_KEY'),
'chat_model' => env('ANTHROPIC_API_CHAT_MODEL', 'claude-3-5-sonnet-20240620'),
],
'ollama' => [
'base_url' => env('OLLAMA_BASE_URL'),
'chat_model' => env('OLLAMA_API_CHAT_MODEL', 'llama3.2'),
'embedding_model' => env('OLLAMA_API_CHAT_MODEL', 'llama3.2'),
],
],
```

Expand Down
8 changes: 7 additions & 1 deletion resources/views/Parts/OutputSchema.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
@isset($outputSchema)
### You must respond using the following schema. Immediately return valid JSON formatted data:
### Mandatory Response Format
Return all responses exclusively in JSON format following this structure with out **ANY** surrounding text:
{!! $outputSchema !!}

Always ensure that the response adheres strictly to this format, as it will be used for API purposes.

### Start Response ###
```json
@endisset
10 changes: 10 additions & 0 deletions resources/views/Prompts/Rat/DraftPrompt.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<message type="user">
### Instruction
Try to answer this question/instruction with step-by-step thoughts and make the answer more structural. Split the answer into several paragraphs.
{!! $question !!}

@include('synapse::Parts.OutputSchema')
</message>



11 changes: 11 additions & 0 deletions resources/views/Prompts/Rat/GetQueryPrompt.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<message type="user">
## User input: {{ $question }}

## Response
{{ $answer }}

## Instruction
Summarize the content with a focus on the last sentences to create a concise search query for Bing. Use search syntax to make the query specific and relevant for content verification.

@include('synapse::Parts.OutputSchema')
</message>
12 changes: 12 additions & 0 deletions resources/views/Prompts/Rat/SplitAnswerPrompt.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<message type="user">
## User input: {{ $question }}

## Response
{{ $answer }}

## Instruction
Split the answer of the question into multiple paragraphs with each paragraph containing a complete thought.
The answer should be splited into less than {{ $number_of_paragraphs }} paragraphs.

@include('synapse::Parts.OutputSchema')
</message>
17 changes: 9 additions & 8 deletions resources/views/Prompts/ReValidateResponsePrompt.blade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Instruction
Your final response did not adhere the required Schema.
** DO NOT EXPLAIN. Only return your final response with the requested format.**

## You must respond in this format:
{!! $outputRules !!}

@if(!empty($errors))
## The following error occurred in the last Rewrite:
## The following error occurred in the last response:
{!! $errors !!}
@endif

@if(!empty($lastResponse))
### Start Last Response ###
{!! $lastResponse !!}
### End Last Response ###
@endif

@include('synapse::Parts.OutputSchema')
4 changes: 2 additions & 2 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
class Agent
{
use HasMiddleware;
use ManagesIntegration,
ManagesTools;
use ManagesIntegration;
use ManagesTools;

/**
* a keyed array of values to be used as extra inputs that are passed to the prompt when it is generated.
Expand Down
76 changes: 76 additions & 0 deletions src/AgentChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace UseTheFork\Synapse;

use UseTheFork\Synapse\AgentTask\PendingAgentChain;
use UseTheFork\Synapse\Traits\HasConfig;
use UseTheFork\Synapse\Traits\Makeable;
use UseTheFork\Synapse\ValueObject\Message;

class AgentChain
{
use Makeable;
use HasConfig;

protected PendingAgentChain $pendingAgentChain;
protected $pipeline;

public function __construct(array $agents)
{
foreach ($agents as $agent) {
if(! ($agent instanceof Agent)){
throw new \Exception("Agent must be an instance of Agent");
}
}

$this->config()->add('persistInputs', []);
$this->config()->add('agents', collect($agents));
$this->pendingAgentChain = $this->createPendingAgentChain();

}

/**
* Create a new PendingAgentTask
*/
public function createPendingAgentChain(): PendingAgentChain
{
return new PendingAgentChain($this);
}

/**
* Handles the user input and extra agent arguments to retrieve the response.
*
* @param array|null $input The input array.
* @param array|null $extraAgentArgs The extra agent arguments array.
* @return Message The final message from the agent.
*
* @throws Throwable
*/
public function handle(?array $input): Message
{
$this->config()->add('input', $input);
$this->config()->get('agents')->each(function ($agent) use ($input) {
$input = [
...$this->config()->get('input'),
...$this->config()->get('persistInputs')
];
$response = $agent->handle($input);
$this->config()->add('input', $response->content());
});

return Message::make([
'role' => 'agent',
'finish_reason' => 'stop',
'content' => $this->config()->get('input')
]);
}

public function persistInputs(array $inputs): static
{
$this->config()->add('persistInputs', $inputs);

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

declare(strict_types=1);

namespace UseTheFork\Synapse\AgentTask;

use UseTheFork\Synapse\AgentChain;
use UseTheFork\Synapse\AgentTask\StartTasks\BootTraits;

class PendingAgentChain
{

protected AgentChain $agentChain;

public function __construct(AgentChain $agentChain)
{
$this->agentChain = $agentChain;

$this
->tap(new BootTraits);

}

/**
* Tap into the agent chain
*
* @return $this
*/
protected function tap(callable $callable): static
{
$callable($this);

return $this;
}

/**
* Retrieve the agent associated with the current task.
*
* @return AgentChain The current agent instance.
*/
public function agent(): AgentChain
{
return $this->agentChain;
}

}
3 changes: 2 additions & 1 deletion src/AgentTask/StartTasks/BootTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace UseTheFork\Synapse\AgentTask\StartTasks;


use UseTheFork\Synapse\AgentTask\PendingAgentChain;
use UseTheFork\Synapse\AgentTask\PendingAgentTask;
use UseTheFork\Synapse\Helpers\Helpers;

Expand All @@ -13,7 +14,7 @@ class BootTraits
/**
* Boot the plugins
*/
public function __invoke(PendingAgentTask $pendingAgentTask): PendingAgentTask
public function __invoke(PendingAgentTask|PendingAgentChain $pendingAgentTask): PendingAgentTask|PendingAgentChain
{

$agent = $pendingAgentTask->agent();
Expand Down
Loading

0 comments on commit b57c957

Please sign in to comment.