Skip to content

Commit

Permalink
Update chat example to use newer template (#10)
Browse files Browse the repository at this point in the history
* Update chat example to use newer template

* Replace JS code with TS code in example README
  • Loading branch information
liamgriffiths authored Aug 22, 2024
1 parent 323ecde commit 2c262ff
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 44 deletions.
14 changes: 8 additions & 6 deletions tutorials/chat-with-an-llm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npm start # Run the example

cd python # Navigate to the python example
poetry install # Install dependencies and build the example
poetry run python src/example.py # Run the example
poetry run main # Run the example
```

</details>
Expand All @@ -39,23 +39,24 @@ we will be including the chat history that includes both the user input and LLM

In NodeJS we can use the following program:

```js
```typescript
import readline from "node:readline";

import { Substrate, ComputeText } from "substrate";

const substrate = new Substrate({ apiKey: process.env["SUBSTRATE_API_KEY"] });

const message = (role, content) => ({ role, content });
type Message = { role: "user" | "assistant", content: string };
const message = (role: Message["role"], content: Message["content"]): Message => ({ role, content });

const blue = (text) => `\x1b[34m${text}\x1b[0m`;
const blue = (text: string) => `\x1b[34m${text}\x1b[0m`;

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const prompt = (log) => `
const prompt = (log: Message[]) => `
You are a friendly assistant. Have fun engaging in the conversation.
=== Rules
Expand All @@ -66,7 +67,7 @@ You are a friendly assistant. Have fun engaging in the conversation.
=== Chat Log
${log.map((m) => `[${m.role}]: ${m.content}`).join("\n\n")}`;

const continueThread = (log) => {
const continueThread = (log: Message[]) => {
rl.question(`> `, async (userPrompt) => {
log.push(message("user", userPrompt));

Expand Down Expand Up @@ -161,3 +162,4 @@ We could extend these simple examples in many different ways to suit a new speci
updating the prompts to guide the LLM responses toward a particular tone or personality. If there is
data we'd like the LLM to reference we could incorportate some context from a relevant data source. Enjoy
exploring what's possible with Substrate.

43 changes: 43 additions & 0 deletions tutorials/chat-with-an-llm/diagram.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
direction: right
classes: {
substrate: {
label: "substrate"
style: {
font: mono
font-color: gray
font-size: 20
stroke: gray
stroke-dash: 1
fill: "transparent"
border-radius: 16
}
}
node: {
style: {
font: mono
font-size: 24
stroke-width: 2
fill: transparent
stroke: gray
border-radius: 16
stroke-dash: 1
3d: true
}
}
edge: {
style: {
stroke: "#000"
stroke-dash: 2
}
}
}

substrate.class: substrate
substrate.a.class: node
substrate.b.class: node
substrate.c.class: node
substrate.a.label: heuristic
substrate.b.label: symbolic
substrate.c.label: computation
substrate.a->substrate.c { class: edge }
substrate.b->substrate.c { class: edge }
111 changes: 111 additions & 0 deletions tutorials/chat-with-an-llm/diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tutorials/chat-with-an-llm/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# python generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# venv
.venv
26 changes: 26 additions & 0 deletions tutorials/chat-with-an-llm/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Substrate Example Templates in Python

This is a Substrate example template written in Python. To run this example,

```bash
# Set your API key as an environment variable.
# Get one here https://www.substrate.run/dashboard/keys if this is your first time.
export SUBSTRATE_API_KEY=<your Substrate API key>

# Navigate to the python example directory.
cd python
```

To run the example with Poetry (default), run the following.

```bash
poetry install
poetry run main
```

To run the example with Rye, comment out the Poetry sections and uncomment the Rye sections in `pyproject.toml` and run the following.

```bash
rye sync
rye run main
```
38 changes: 19 additions & 19 deletions tutorials/chat-with-an-llm/python/poetry.lock

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

1 change: 0 additions & 1 deletion tutorials/chat-with-an-llm/python/poetry.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[virtualenvs]
in-project = true

10 changes: 7 additions & 3 deletions tutorials/chat-with-an-llm/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[project]
name = "chat-with-an-llm"
name = "template"
version = "0.1.0"
description = ""
authors = [{ name = "Hanwen Wu", email = "[email protected]" }]
readme = "README.md"

requires-python = ">= 3.9"
dependencies = ["substrate"]

Expand Down Expand Up @@ -28,7 +32,7 @@ build-backend = "poetry.core.masonry.api"
name = "template"
version = "0.1.0"
description = ""
authors = []
authors = ["Hanwen Wu <[email protected]>"]
readme = "README.md"

packages = [{ include = "python", from = "src" }]
Expand All @@ -42,7 +46,7 @@ ruff = "^0.6.1"
marimo = "^0.8.0"

[tool.poetry.scripts]
main = "example:main"
main = "python.example:main"


###########################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ def main():

if __name__ == "__main__":
main()

28 changes: 28 additions & 0 deletions tutorials/chat-with-an-llm/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Substrate Example Templates in Typescript

This is a Substrate example template written in Typescript. To run this example,

```bash
# Set your API key as an environment variable.
# Get one here https://www.substrate.run/dashboard/keys if this is your first time.
export SUBSTRATE_API_KEY=<your Substrate API key>

# Navigate to the python example directory.
cd typescript
```

To run the example with tsx (default), run the following.

```bash
npx tsx ./example.ts

# Or you can use the package.json scripts
npm run start
```

To run the example with Deno, uncomment the Deno sections in `example.ts` and
run the following.

```bash
deno run ./example.ts
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { Substrate, ComputeText } from "substrate";

const substrate = new Substrate({ apiKey: process.env["SUBSTRATE_API_KEY"] });

const message = (role, content) => ({ role, content });
type Message = { role: "user" | "assistant", content: string };
const message = (role: Message["role"], content: Message["content"]): Message => ({ role, content });

const blue = (text) => `\x1b[34m${text}\x1b[0m`;
const blue = (text: string) => `\x1b[34m${text}\x1b[0m`;

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const prompt = (log) => `
const prompt = (log: Message[]) => `
You are a friendly assistant. Have fun engaging in the conversation.
=== Rules
Expand All @@ -24,7 +25,7 @@ You are a friendly assistant. Have fun engaging in the conversation.
=== Chat Log
${log.map((m) => `[${m.role}]: ${m.content}`).join("\n\n")}`;

const continueThread = (log) => {
const continueThread = (log: Message[]) => {
rl.question(`> `, async (userPrompt) => {
log.push(message("user", userPrompt));

Expand All @@ -46,3 +47,4 @@ const continueThread = (log) => {
};
console.log("=== Welcome to the chat! (use ^C to quit)");
continueThread([]);

32 changes: 25 additions & 7 deletions tutorials/chat-with-an-llm/typescript/package-lock.json

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

15 changes: 11 additions & 4 deletions tutorials/chat-with-an-llm/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"name": "chat-with-an-llm",
"name": "substrate-example",
"version": "1.0.0",
"private": "true",
"main": "example.ts",
"type": "module",
"scripts": {
"start": "node main.js"
"start": "npx tsx example.ts",
"typecheck": "tsc --noEmit"
},
"license": "MIT",
"dependencies": {
"substrate": "^120240617.1.9"
"substrate": "^120240617"
},
"devDependencies": {
"@types/node": "^22",
"typescript": "^5"
}
}
20 changes: 20 additions & 0 deletions tutorials/chat-with-an-llm/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"moduleDetection": "force",
"module": "Preserve",
"resolveJsonModule": true,
"allowJs": true,
"esModuleInterop": true,
"skipLibCheck": true,
"isolatedModules": true,
"strict": true,
"noEmit": true,
"moduleResolution": "node",
"jsx": "preserve",
"incremental": true,
"plugins": [{ "name": "next" }],
"paths": { "@/*": ["./*"] }
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 2c262ff

Please sign in to comment.