Search

28 results for openai function calling

Code

28
View more
const INSTRUCTIONS = `
the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{
Give them a very brief summary of the benefits of the Realtime API based on the details below,
When the user says goodbye or you think the call is over, say a brief goodbye and then
invoke the end_call function.
---
The new ${MODEL} model is the first Realtime API model with built-in reasoning capability, whi
Tool calling is much more robust, including a major reduction in hallucinated tool calls.
`;
const HANGUP_TOOL = {
type: "function",
name: "end_call",
description: `
Use this function to hang up the call when the user says goodbye
or otherwise indicates they are about to end the call.`,
// Builds the declarative session configuration for a Realtime API session.
export function makeSession(modelOverride?: string) {
const model = modelOverride || MODEL;
// AgentHandler implements the agent runtime behavior (tool calling, etc).
class AgentHandler implements ObserverHandler {
// Creates the runtime handler for the session.
export function createHandler(client: ObserverClient, callId: string) {
return new AgentHandler(client, callId);
You can chat with llms over email, the email thread functions as memory. The biggest thing is th
ke interface with llms. Pair that with back end data and functions and you got something really
### Toolings
* Llms can uses [tools](https://platform.openai.com/docs/guides/function-calling), meaning you c
import { OpenAI } from "npm:openai";
const openai = new OpenAI();
const functionExpression = await openai.chat.completions.create({
"messages": [
],
"functions": [
{
});
console.log(functionExpression);
// TODO pull out function call and initial message
let args = functionExpression.choices[0].message.function_call.arguments;
let functionCallResult = { "temperature": "22", "unit": "celsius", "description": "Sunny" };
const result = await openai.chat.completions.create({
"messages": [
"content": null,
"function_call": { "name": "get_current_weather", "arguments": "{ \"location\": \"Boston,
},
{
"role": "function",
"name": "get_current_weather",
"content": JSON.stringify(functionCallResult),
},
],
"functions": [
{
Migrated from folder: External_APIs/openai/function_calling/gpt4FunctionCallingExample
Built on the OpenAI Realtime API (WebRTC) with server-side function calling.
Mashes up patterns from
B[Browser] -->|SDP offer| RTC[/rtc/]
RTC -->|WebRTC| OAI[OpenAI Realtime API]
RTC -->|spawn| OBS[/observer/]
- **Browser** opens a WebRTC peer connection to OpenAI via `/rtc`. The dashboard
itself renders inside a sandboxed iframe pointing at `/api/view`.
- **`/rtc`** forwards the SDP offer to OpenAI and kicks off the observer for the
resulting call id (fire-and-forget).
- **`/observer/:callId`** opens a server-side websocket to the same Realtime
session. When the model emits `response.function_call_arguments.done`, the
observer dispatches the tool against SQLite and posts a
`function_call_output` back, then triggers a new response.
- **Frontend polls `/api/current`** every 1.5s. When the active view name or
│ ├── api.ts # REST for views/versions/rules
│ └── utils.ts # OpenAI URL/header helpers
└── database/
1. Set `OPENAI_API_KEY` in this val's env (the GA Realtime API key from
[platform.openai.com](https://platform.openai.com)).
2. Open the val's HTTP endpoint. The Welcome view is seeded on first request.
// elaine/contracts.py's own recursive vocabulary, serialized by
// elaine/api/catalog.py::_value_contract_record()) into an OpenAI/Mistral
// function-calling-compatible JSON Schema, so a tool's `parameters` are
// derived from the live catalog contract instead of hand-written text that
// MEAN_TOOL (teaching/plainChat.ts) doesn't do today, and what this is for.
export function valueContractToJsonSchema(contract: ValueContractRecord): Record<string, unknown
if (contract.kind === "scalar") {
export function buildToolDefinitionFromCatalog(
toolName: string,
Val Town's standard-library integrations — `std/openai`, `std/fetch` — are not platform magic. E
ia `https://esm.town/v/...`. It wraps your API in a nice function and authenticates the *user* t
2. **A proxy val** (an HTTP val) that verifies who's calling, applies limits, attaches **your**
are needed — which means a partner can ship a sponsored function (e.g. `sent-dm/sms`) entirely
The SDK's only jobs: expose a clean function, and pass along the caller's identity. Every Val To
export async function doTheThing(input: string): Promise<Result> {
const token = Deno.env.get("valtown");
- **`std/fetch` style** — a plain function, as above. Best for simple APIs.
- **`std/openai` style** — subclass an existing SDK and point its `baseURL` at your proxy, so us
```ts
import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";
export class OpenAI extends RawOpenAI {
constructor(options: ClientOptions = {}) {
...options,
baseURL: "https://std-openaiproxy.web.val.run/v1",
apiKey: Deno.env.get("valtown"),
```ts
async function verifyToken(token: string): Promise<{ id: string; username: string; tier: string
const res = await fetch("https://api.val.town/v1/me", {
export default async function handler(req: Request): Promise<Response> {
const authHeader = req.headers.get("Authorization");
export async function checkRateLimit(userId: string, max: number, windowSeconds: number) {
const result = await sqlite.execute({
export async function logUsage(userId: string, username: string, statusCode: number | null) {
await sqlite.execute({
Tips from `std/openaiproxy`, which serves every Val Town user:
async function forward(req: Request, pathname: string): Promise<Response> {
if (!allowedPathnames.includes(pathname)) {
- **Strip identifying headers** from the upstream response before returning it.
some parameters shouldn't be user-controllable (e.g. `std/openaiproxy` rewrites `model` for free
```ts
export default async function handler(req: Request): Promise<Response> {
try {
const INSTRUCTIONS = `
the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{
Give them a very brief summary of the benefits of the Realtime API based on the details below,
When the user says goodbye or you think the call is over, say a brief goodbye and then
invoke the end_call function.
---
The new ${MODEL} model is the first Realtime API model with built-in reasoning capability, whi
Tool calling is much more robust, including a major reduction in hallucinated tool calls.
`;
const HANGUP_TOOL = {
type: "function",
name: "end_call",
description: `
Use this function to hang up the call when the user says goodbye
or otherwise indicates they are about to end the call.`,
// Builds the declarative session configuration for a Realtime API session.
export function makeSession(modelOverride?: string) {
const model = modelOverride || MODEL;
// AgentHandler implements the agent runtime behavior (tool calling, etc).
class AgentHandler implements ObserverHandler {
// Creates the runtime handler for the session.
export function createHandler(client: ObserverClient, callId: string) {
return new AgentHandler(client, callId);
const INSTRUCTIONS = `
the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{
Give them a very brief summary of the benefits of the Realtime API based on the details below,
When the user says goodbye or you think the call is over, say a brief goodbye and then
invoke the end_call function.
---
The new ${MODEL} model is the first Realtime API model with built-in reasoning capability, whi
Tool calling is much more robust, including a major reduction in hallucinated tool calls.
`;
const HANGUP_TOOL = {
type: "function",
name: "end_call",
description: `
Use this function to hang up the call when the user says goodbye
or otherwise indicates they are about to end the call.`,
// Builds the declarative session configuration for a Realtime API session.
export function makeSession(modelOverride?: string) {
const model = modelOverride || MODEL;
// AgentHandler implements the agent runtime behavior (tool calling, etc).
class AgentHandler implements ObserverHandler {
// Creates the runtime handler for the session.
export function createHandler(client: ObserverClient, callId: string) {
return new AgentHandler(client, callId);
// Configure the timer with the 🕒 icon in the top right.
// Sends a "good morning" email with a weather update using OpenAI tool calling.
import { OpenAI } from "https://esm.town/v/std/openai";
import { email } from "https://esm.town/v/std/email";
const openai = new OpenAI();
async function getCurrentWeather(location: string): Promise<string> {
const WEATHER_API_KEY = process.env.WEATHER_API_KEY ?? "";
async function sendEmail(subject: string, content: string): Promise<string> {
await email({ subject, text: content });
// --- Tool definitions for OpenAI ---
const tools: OpenAI["beta"]["chat"]["completions"]["parse"] extends never ? never : any[] = [
{
type: "function",
function: {
name: "get_current_weather",
{
type: "function",
function: {
name: "send_email",
async function runAgent(userMessage: string) {
const messages: any[] = [
// Loop until the model stops calling tools
for (let i = 0; i < 5; i++) {
const response = await openai.chat.completions.create({
model: "gpt-4o-mini",
for (const toolCall of assistantMessage.tool_calls) {
const args = JSON.parse(toolCall.function.arguments);
let result: string;
switch (toolCall.function.name) {
case "get_current_weather":
default:
result = `Unknown tool: ${toolCall.function.name}`;
}
console.log(`Tool [${toolCall.function.name}]:`, result);
export default async function scheduledHandler() {
await runAgent(
Built on the OpenAI Realtime API (WebRTC) with server-side function calling.
Mashes up patterns from
B[Browser] -->|SDP offer| RTC[/rtc/]
RTC -->|WebRTC| OAI[OpenAI Realtime API]
RTC -->|spawn| OBS[/observer/]
- **Browser** opens a WebRTC peer connection to OpenAI via `/rtc`. The dashboard
itself renders inside a sandboxed iframe pointing at `/api/view`.
- **`/rtc`** forwards the SDP offer to OpenAI and kicks off the observer for the
resulting call id (fire-and-forget).
- **`/observer/:callId`** opens a server-side websocket to the same Realtime
session. When the model emits `response.function_call_arguments.done`, the
observer dispatches the tool against SQLite and posts a
`function_call_output` back, then triggers a new response.
- **Frontend polls `/api/current`** every 1.5s. When the active view name or
│ ├── api.ts # REST for views/versions/rules
│ └── utils.ts # OpenAI URL/header helpers
└── database/
1. Set `OPENAI_API_KEY` in this val's env (the GA Realtime API key from
[platform.openai.com](https://platform.openai.com)).
2. Open the val's HTTP endpoint. The Welcome view is seeded on first request.
# OpenAI-Compatible API Proxy v2.0
A production-ready, non-streaming backend that exposes OpenAI-compatible endpoints for both **Op
OpenAI models work out-of-the-box via Val Town's built-in proxy (no key needed).
### `GET /api/v1/models`
Returns all available models in OpenAI format. No auth required.
### OpenAI models (built-in, no key needed)
### Non-streaming chat (OpenAI)
```bash
### Tool calling (Claude)
```bash
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
### Multi-turn tool calling (returning tool result)
```bash
{"role": "user", "content": "Whats the weather in Beijing?"},
ntent": null, "tool_calls": [{"id": "call_123", "type": "function", "function": {"name": "get_we
{"role": "tool", "tool_call_id": "call_123", "content": "{\"temp\": 22, \"condition\": \"S
### Python (OpenAI SDK)
```python
from openai import OpenAI
client = OpenAI(
api_key="any-token",
tools = [{
"type": "function",
"function": {
"name": "get_weather",
if msg.tool_calls:
print("Tool called:", msg.tool_calls[0].function.name)
print("Arguments:", msg.tool_calls[0].function.arguments)
```
- **Streaming is disabled** — set `stream: false` or omit it entirely.
hropic `auto`, `"required"` → Anthropic `any`, `{"type":"function","function":{"name":"..."}}` →
- **system messages** are correctly lifted to Anthropic's top-level `system` parameter.
const INSTRUCTIONS = `
the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{
Give them a very brief summary of the benefits of the Realtime API based on the details below,
When the user says goodbye or you think the call is over, say a brief goodbye and then
invoke the end_call function.
---
5 model offers more reliable instruction following, tool calling, and multilingual accuracy.
Specifically, the model delivers a +5% intelligence lift on Big Bench Audio, which measures re
const HANGUP_TOOL = {
type: "function",
name: "end_call",
description: `
Use this function to hang up the call when the user says goodbye
or otherwise indicates they are about to end the call.`,
// Builds the declarative session configuration for a Realtime API session.
export function makeSession(modelOverride?: string) {
const model = modelOverride || MODEL;
// AgentHandler implements the agent runtime behavior (tool calling, etc).
class AgentHandler implements ObserverHandler {
// Creates the runtime handler for the session.
export function createHandler(client: ObserverClient, callId: string) {
return new AgentHandler(client, callId);
const INSTRUCTIONS = `
the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{
Give them a very brief summary of the benefits of the Realtime API based on the details below,
When the user says goodbye or you think the call is over, say a brief goodbye and then
invoke the end_call function.
---
5 model offers more reliable instruction following, tool calling, and multilingual accuracy.
Specifically, the model delivers a +5% intelligence lift on Big Bench Audio, which measures re
const HANGUP_TOOL = {
type: "function",
name: "end_call",
description: `
Use this function to hang up the call when the user says goodbye
or otherwise indicates they are about to end the call.`,
// Builds the declarative session configuration for a Realtime API session.
export function makeSession(modelOverride?: string) {
const model = modelOverride || MODEL;
// AgentHandler implements the agent runtime behavior (tool calling, etc).
class AgentHandler implements ObserverHandler {
// Creates the runtime handler for the session.
export function createHandler(client: ObserverClient, callId: string) {
return new AgentHandler(client, callId);
const INSTRUCTIONS = `
the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{
Give them a very brief summary of the benefits of the Realtime API based on the details below,
When the user says goodbye or you think the call is over, say a brief goodbye and then
invoke the end_call function.
---
5 model offers more reliable instruction following, tool calling, and multilingual accuracy.
Specifically, the model delivers a +5% intelligence lift on Big Bench Audio, which measures re
const HANGUP_TOOL = {
type: "function",
name: "end_call",
description: `
Use this function to hang up the call when the user says goodbye
or otherwise indicates they are about to end the call.`,
// Builds the declarative session configuration for a Realtime API session.
export function makeSession(modelOverride?: string) {
const model = modelOverride || MODEL;
// AgentHandler implements the agent runtime behavior (tool calling, etc).
class AgentHandler implements ObserverHandler {
// Creates the runtime handler for the session.
export function createHandler(client: ObserverClient, callId: string) {
return new AgentHandler(client, callId);

Vals

No vals found

Users

No users found