PHPackages                             amohamed/jsconnector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. amohamed/jsconnector

ActiveProject[Utility &amp; Helpers](/categories/utility)

amohamed/jsconnector
====================

JSConnector Laravel Package provides an easy way to integrate the JSConnector JavaScript library into your Laravel application. This package offers a simple facade to interact with the JSConnector library, enabling developers to leverage the power of OpenAI's language models in their Laravel projects without the hassle of directly managing communication between PHP and JavaScript.

v1.0.5(2y ago)317MITPHPPHP &gt;=7.2

Since Jun 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Abdallah-Tah/jsconnector)[ Packagist](https://packagist.org/packages/amohamed/jsconnector)[ RSS](/packages/amohamed-jsconnector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (7)Versions (7)Used By (0)

Laravel JS Connector
====================

[](#laravel-js-connector)

The Laravel JS Connector package provides an easy way to integrate the JSConnector JavaScript library into your Laravel application. This package offers a simple facade to interact with the JSConnector library, enabling developers to leverage the power of OpenAI's language models in their Laravel projects without the hassle of directly managing communication between PHP and JavaScript.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require amohamed/jsconnector
```

Configuration
-------------

[](#configuration)

Publish the config file with:

```
php artisan vendor:publish --provider="Amohamed\JSConnector\JSConnectorServiceProvider" --tag="config"
```

This is the contents of the config file:

```
return [
    'api_url' => env('JS_CONNECTOR_API_URL', 'http://localhost:3000/api'),
    'retry_times' => env('JS_CONNECTOR_RETRY_TIMES', 3),
    'retry_interval' => env('JS_CONNECTOR_RETRY_INTERVAL', 100),
];
```

You can customize the values in the .env file like so:

```
JS_CONNECTOR_API_URL=http://localhost:3000/api
JS_CONNECTOR_RETRY_TIMES=3
JS_CONNECTOR_RETRY_INTERVAL=100
```

Starting and Stopping the Node.js Server
----------------------------------------

[](#starting-and-stopping-the-nodejs-server)

Before you can start making requests with JSConnector, you need to ensure that the Node.js server is running. You can start the server with the provided artisan command:

```
php artisan jsconnector:serve
```

To stop the running server, you can use:

```
php artisan jsconnector:stop
```

Usage
-----

[](#usage)

Here is a basic example of how to use the JS Connector:

```
$response = JSConnector::post('test-endpoint', ['baz' => 'qux']);
```

Usage with LangChain JS
-----------------------

[](#usage-with-langchain-js)

To use JSConnector with LangChain JS in your Laravel application, you need to install LangChain JS first. You can do this by running:

```
npm install -S langchain
```

Then, on your Node.js server, you can create a JavaScript file where you import and initialize LangChain:

```
require('dotenv').config();

const { OpenAI } = require('langchain/llms/openai');
const { BufferMemory } = require('langchain/memory');
const { ConversationChain } = require('langchain/chains');

const model = new OpenAI({ key: process.env.OPENAI_API_KEY });
const memory = new BufferMemory();
const chain = new ConversationChain({ llm: model, memory: memory });
const cors = require('cors');

const express = require('express');
const app = express();
app.use(cors());

app.use(express.json());

app.post('/chat', async (req, res) => {
    console.log(`Request body: ${JSON.stringify(req.body)}`);
    const result = await chain.call({ input: req.body.input });
    console.log(`API response: ${JSON.stringify(result)}`);
    res.send(result);
});

app.listen(3000, () => {
    console.log('Langchain server running on port 3000');
});
```

In your Laravel application, you can use the JSConnector to send data to the LangChain JS service:

```
