Skip to content

Commit 78cd1da

Browse files
chore(release): v0.0.0-alpha.0
0 parents  commit 78cd1da

23 files changed

+5626
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Code Coverage
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
nodejs:
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-version: [18.x,20.x]
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v2
21+
22+
- name: Set up Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Run the tests
31+
run: npm run test:coverage

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dist
2+
node_modules
3+
*.tsbuildinfo
4+
.coverage
5+
types
6+
browser

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (c) 2023 We Can Do Better
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 🏛️ Delphi
2+
3+
Delphi is a versatile and adaptable conversational agent framework that
4+
streamlines the creation and management of chatbots and AI-driven communication
5+
systems. Its design prioritizes abstraction and provider-independence, allowing
6+
for seamless integration with various message providers. Delphi is compatible
7+
with a range of environments, including browsers, Node.js, and Deno, making it
8+
an ideal choice for diverse application scenarios.
9+
10+
## 🚀 Features
11+
12+
- 🤖 **Provider-Independent Messaging:** Delphi abstracts the messaging
13+
function, allowing seamless integration with various providers like OpenAI and
14+
Azure OpenAI.
15+
- 🔄 **Flexible Conversational Flow:** Manages complex conversation patterns
16+
with ease, facilitating intelligent and context-aware interactions.
17+
- 🛠️ **Customizable Agent Options:** Tailor your agent with configurable settings
18+
such as maximum rounds and specific model preferences.
19+
- 🌍 **Cross-Platform Compatibility:** Works effortlessly in browsers, Node.js,
20+
and Deno environments.
21+
- 📚 **Context Management:** Efficiently maintains conversation states,
22+
enhancing the continuity and relevance of interactions.
23+
- 📝 **Robust Schema Validation:** Ensures the integrity and correctness of data
24+
with JSON schema validation.
25+
- 🎛️ **Modular Design:** Facilitates easy integration and scalability through a
26+
clear and modular code structure.
27+
28+
## 📌 Requirements
29+
30+
- Node.js (v18 or newer), a modern browser, or Deno
31+
- TypeScript
32+
- An account with a supported message provider (e.g., Azure, OpenAI)
33+
34+
## 🛠️ Installation
35+
36+
Delphi is available on npm and can be easily installed:
37+
38+
```bash
39+
npm install @wecandobetter/delphi
40+
```
41+
42+
## 🚀 Getting Started
43+
44+
To quickly get started with Delphi:
45+
46+
1. **Import Delphi:** Import the necessary components from Delphi in your
47+
project.
48+
```typescript
49+
import { Agent, Context } from "@wecandobetter/delphi";
50+
```
51+
52+
2. **Create an Agent:** Instantiate an `Agent` with your desired configuration.
53+
```typescript
54+
const agent = new Agent("myAgent" /* ... configuration ... */);
55+
```
56+
57+
3. **Initialize Context:** Set up a `Context` to manage the conversation state.
58+
```typescript
59+
const context = new Context();
60+
61+
context.addMessage({
62+
role: "system",
63+
content: "You are a helpful assistant!",
64+
});
65+
```
66+
67+
4. **Run the Agent:** Start the agent with the initialized context.
68+
```typescript
69+
const message = await agent.run(context);
70+
console.log(message.content);
71+
```
72+
73+
This example gives you a basic setup for initiating a conversation with Delphi.
74+
For more detailed usage and advanced features, refer to the documentation.
75+
76+
## 📖 Documentation
77+
78+
Visit our [Documentation Page](./docs) for information on API usage and advanced
79+
features.
80+
81+
## 🤝 Contributing
82+
83+
Contributions are welcome! Create an issue, open a pull request, or reach out to
84+
us in the discussions section.
85+
86+
## 📜 License
87+
88+
Delphi is released under the MIT License. See the [LICENSE](LICENSE) file for
89+
more details.
90+
91+
## ✉️ Contact
92+
93+
Reach out to us at [contact@wcdb.life](mailto:contact@wcdb.life) for support or
94+
inquiries.
95+
96+
Happy coding! 🎉

docs/agent_functions.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 📘 Delphi Tutorial: Using Agent Functions
2+
3+
In Delphi, agent functions are a versatile tool allowing your conversational
4+
agent to execute specific tasks, like data retrieval or complex computations,
5+
dynamically during a conversation. Let's dive into how you can utilize these
6+
functions in your Delphi projects.
7+
8+
## Step 1: Setting Up Your Environment
9+
10+
Before beginning, ensure you have Delphi and its dependencies installed in your
11+
project. If you haven't already set up Delphi, refer to the 'Getting Started'
12+
section of the Delphi README.
13+
14+
## Step 2: Define an Agent Function
15+
16+
An agent function in Delphi takes an input, performs an operation, and returns
17+
an output. To define one:
18+
19+
1. **Create a New Function Class**: Define a new class for your function.
20+
21+
2. **Specify Input Schema**: Define a JSON schema for the input. This schema is
22+
used to validate the data your function will receive.
23+
24+
3. **Implement the Function Logic**: Write the core logic of your function in
25+
the constructor.
26+
27+
Example:
28+
29+
```typescript
30+
import { AgentFunction, type JSONSchemaType } from "@wecandobetter/delphi";
31+
32+
interface ExampleInput {
33+
query: string;
34+
}
35+
36+
const exampleInputSchema: JSONSchemaType<ExampleInput> = {
37+
type: "object",
38+
properties: {
39+
query: { type: "string" },
40+
},
41+
required: ["query"],
42+
};
43+
44+
const exampleFunction = new AgentFunction<ExampleInput, string>(
45+
"exampleFunction",
46+
"This is an example function",
47+
exampleInputSchema,
48+
async ({ query }) => {
49+
// Your function logic here, returning a string
50+
return `You queried: ${query}`;
51+
},
52+
);
53+
```
54+
55+
## Step 3: Register and Enable the Function
56+
57+
With your function defined, the next step is to register it with your agent's
58+
context and enable it.
59+
60+
```typescript
61+
const context = new Context();
62+
63+
context.addFunction(exampleFunction);
64+
context.functions.enable(exampleFunction.name);
65+
```
66+
67+
## Conclusion
68+
69+
Agent functions in Delphi provide a powerful way to extend the capabilities of
70+
your conversational agents. By defining custom functions, registering them with
71+
your agent, and then utilizing them in conversation flows, you can create rich,
72+
interactive, and dynamic conversational experiences. The key is to tailor these
73+
functions to fit the specific needs and contexts of your application. Happy
74+
coding!

docs/agents/wikipedia.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# 📘 Delphi Tutorial: Creating a Wikipedia Search Agent
2+
3+
In this tutorial, we'll guide you through creating a conversational agent using
4+
Delphi that can perform Wikipedia searches. This agent will leverage the Azure
5+
OpenAI client for communication and a custom agent function for searching
6+
Wikipedia.
7+
8+
## 📚 Prerequisites
9+
10+
- Ensure you have Node.js installed.
11+
- Set up a project with TypeScript.
12+
- Install the `@azure/openai` and `wikipedia` npm packages.
13+
14+
## Step 1: Import Dependencies
15+
16+
First, import all the necessary modules at the top of your TypeScript file:
17+
18+
```typescript
19+
import { OpenAIClient, OpenAIKeyCredential } from "@azure/openai";
20+
import wikipedia from "wikipedia";
21+
22+
import {
23+
Agent,
24+
AgentFunction,
25+
Context,
26+
type JSONSchemaType,
27+
} from "@wecandobetter/delphi";
28+
```
29+
30+
## Step 2: Define Search Parameters and Results
31+
32+
Define interfaces for your search parameters and the expected search results:
33+
34+
```typescript
35+
interface SearchParameters {
36+
query: string;
37+
limit?: number;
38+
suggestion?: boolean;
39+
}
40+
41+
interface SearchResult {
42+
results: string[];
43+
suggestion?: string;
44+
}
45+
```
46+
47+
## Step 3: Create a JSON Schema
48+
49+
Define a JSON schema for validating the input to your search function:
50+
51+
```typescript
52+
const searchSchema: JSONSchemaType<SearchParameters> = {
53+
type: "object",
54+
properties: {
55+
query: { type: "string" },
56+
limit: { type: "number", nullable: true },
57+
suggestion: { type: "boolean", nullable: true },
58+
},
59+
required: ["query"],
60+
additionalProperties: false,
61+
};
62+
```
63+
64+
## Step 4: Implement the Search Function
65+
66+
Create an instance of `AgentFunction` that performs the Wikipedia search:
67+
68+
```typescript
69+
const searchFn = new AgentFunction<SearchParameters, SearchResult>(
70+
"search",
71+
"Search on Wikipedia",
72+
searchSchema,
73+
async ({ query, limit = 5, suggestion = true }) => {
74+
const { results, suggestion: sugg } = await wikipedia.search(query, {
75+
limit,
76+
suggestion,
77+
});
78+
return { results: results.map(({ title }) => title), suggestion: sugg };
79+
},
80+
);
81+
```
82+
83+
## Step 5: Set Up the OpenAI Client
84+
85+
Configure the OpenAI client with your API key:
86+
87+
```typescript
88+
const client = new OpenAIClient(new OpenAIKeyCredential("<API_KEY>"));
89+
```
90+
91+
## Step 6: Create the Agent
92+
93+
Instantiate an `Agent` that will handle chat completions:
94+
95+
```typescript
96+
const agent = new Agent(
97+
"wiki",
98+
async (messages, options) => {
99+
const response = await client.getChatCompletions(
100+
options.model,
101+
messages,
102+
options,
103+
);
104+
return response.choices[0].message!;
105+
},
106+
{ client: { model: "gpt-4-1106-preview" } },
107+
);
108+
```
109+
110+
## Step 7: Initialize the Context
111+
112+
Create a new `Context` and add a system message to it:
113+
114+
```typescript
115+
const context = new Context();
116+
context.addMessage({
117+
role: "system",
118+
content:
119+
"Search Wikipedia for a random scientific subject and tell me about it.",
120+
});
121+
```
122+
123+
## Step 8: Register and Enable the Function
124+
125+
Register your search function in the context and enable it:
126+
127+
```typescript
128+
context.functions.set(searchFn.name, searchFn);
129+
context.functions.enable(searchFn.name);
130+
```
131+
132+
## Step 9: Run the Agent and Display the Result
133+
134+
Finally, run the agent with the context and log the result:
135+
136+
```typescript
137+
const result = await agent.run(context);
138+
console.log(result.content);
139+
```
140+
141+
## 🎉 Conclusion
142+
143+
Congratulations! You've successfully created a Delphi agent that can search
144+
Wikipedia based on user prompts and display the results. This tutorial showcases
145+
the integration of custom agent functions, use of external APIs, and
146+
conversation management in Delphi. Feel free to expand and customize your agent
147+
to suit your specific needs.

0 commit comments

Comments
 (0)