Skip to content

Commit

Permalink
I154 Onboard claude (#5)
Browse files Browse the repository at this point in the history
* Extract default openai-request to a library.

* Run claude somehow.

* Move to "shared".

* Run claude test.

* Upgrade jsonnet package to support "files".

* Pass Claude test.

* Commit missing boilerplate files.

* Fine-tune paths.

* Openai template: use the library.

* Move openai document template to the library.

* Mass-test.

* Describe how to run using jsonnet command line.

* Make a distro with the lib and anthropic.

* Update example of using jsonnet package.

* Upgrade version in apis package.

* Pass the tests.

* Operative adds.

* Lint.

* Pass the test.

* Lint.

* Add note about template loading in nodejs.

* Build new "tplfa" package.

* `loadTemplate`: use the path to the template, not a name.

* Use "loadTemplate" from the library.

* Get a response from openai.

* Add Claude document template.

* Make dist for APIs.

* Prepare "jsonnet" for release.

* Mass-deps in "apis".

* Release "apis".

* Release "tplfa".
  • Loading branch information
olpa authored Oct 2, 2024
1 parent c4d0141 commit 7f96e39
Show file tree
Hide file tree
Showing 57 changed files with 1,967 additions and 127 deletions.
1 change: 1 addition & 0 deletions apis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
26 changes: 25 additions & 1 deletion apis/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
LIB_FILES :=$(wildcard lib/*.jsonnet)

OPENAI_FILES := $(wildcard openai/lib/*.jsonnet) \
$(wildcard openai/fixture/*.json)

SCHEMA_FILES := $(wildcard schemas/*.json)

ALL_FILES := $(OPENAI_FILES) $(SCHEMA_FILES)
ANTHROPIC_FILES := $(wildcard anthropic/lib/*.jsonnet)

DOC_FILES := README.md changelog.md

ALL_FILES := $(DOC_FILES) $(SCHEMA_FILES) $(LIB_FILES) $(OPENAI_FILES) $(ANTHROPIC_FILES)
ALL_DIST_FILES := $(addprefix dist/, $(ALL_FILES))

dist: $(ALL_DIST_FILES)

./dist/%: %
mkdir -p $(dir $@)
cp $< $@

# --

TEST_DIRS := $(dir $(wildcard */test))

TEST_TRIGGERS := $(addsuffix .test-trigger, $(TEST_DIRS))

test: $(TEST_TRIGGERS)

%.test-trigger:
cd $(dir $@) && npm test

DEPS_TRIGGERS := $(addsuffix .deps-trigger, $(TEST_DIRS))

update-deps: $(DEPS_TRIGGERS)

%.deps-trigger:
cd $(dir $@) && npm install tplfa-jsonnet --update
34 changes: 33 additions & 1 deletion apis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@ npm ci # once to setup the test project
npm test # run tests
```

## Sample run

Using [jsonnet](https://jsonnet.org/) command-line tool:

```
jsonnet --jpath ./lib ./openai/lib/request-tpl.jsonnet -V prompt=hello \
-V secret1=TOKEN -V secret2=orgid
```

Output:

```
{
"body": {
"messages": [
{
"content": "hello",
"role": "user"
}
],
"model": "gpt-3.5-turbo"
},
"headers": {
"Authorization": "Bearer TOKEN",
"Content-type": "application/json",
"OpenAI-Organization": "orgid"
},
"method": "POST",
"url": "https://api.openai.com/v1/chat/completions"
}
```

## Request templates

Input:
Expand Down Expand Up @@ -70,5 +102,5 @@ The document structure will be more rich in the future. It will be based on the
# Using in NPM

```
npm install https://github.com/olpa/templating-for-api/releases/download/apis-v1.0.0/apis-v1.0.0.tar.gz
npm install https://github.com/olpa/templating-for-api/releases/download/apis-v1.1.1/apis-v1.1.1.tar.gz
```
18 changes: 18 additions & 0 deletions apis/anthropic/fixture/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": "msg_015ZPFSNXEvVHRymsG7H6Pab",
"type": "message",
"role": "assistant",
"model": "claude-3-5-sonnet-20240620",
"content": [
{
"type": "text",
"text": "Pong!"
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 17,
"output_tokens": 55
}
}
16 changes: 16 additions & 0 deletions apis/anthropic/lib/document-tpl.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local response = std.parseJson(std.extVar("response"));

{
doc: [
{
type: 'markdown',
content: [
{
type: 'text',
text: item.text
}
]
}
for item in response.content
]
}
11 changes: 11 additions & 0 deletions apis/anthropic/lib/request-tpl.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local openai = import "openai-request-tpl.jsonnet";

openai.run("https://api.anthropic.com/v1/messages", "key") + {
headers+: {
"Anthropic-version": "2023-06-01",
},
body+: {
"max_tokens": 1024,
"model": "claude-3-5-sonnet-20240620",
}
}
Loading

0 comments on commit 7f96e39

Please sign in to comment.