8000 fix: use model provider credentials by njhale · Pull Request #459 · obot-platform/tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: use model provider credentials #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions browser/package-lock.json

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

2 changes: 1 addition & 1 deletion browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"env-paths": "^3.0.0",
"express": "^4.18.2",
"global-cache-dir": "^6.0.0",
"playwright": "^1.48.1",
"playwright": "^1.50.1",
"sharp": "^0.33.5",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
Expand Down
11 changes: 9 additions & 2 deletions browser/src/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GPTScript } from '@gptscript-ai/gptscript'
import { delay } from './delay.ts'
import { URL } from 'url'
import TurndownService from 'turndown'
import { ModelProviderCredentials } from './session.ts'

export async function close (page: Page): Promise<void> {
await page.close()
Expand Down Expand Up @@ -150,7 +151,7 @@ export async function filterContent (page: Page, tabID: string, printTabID: bool
}

// inspect inspects a webpage and returns a locator for a specific element based on the user's input and action.
export async function inspect (page: Page, model: string, userInput: string, action: string, matchTextOnly: boolean, keywords?: string[]): Promise<string[]> {
export async function inspect (page: Page, modelProviderCredentials: ModelProviderCredentials | undefined, model: string, userInput: string, action: string, matchTextOnly: boolean, keywords?: string[]): Promise<string[]> {
if (userInput === '') {
// This shouldn't happen, since the LLM is told that it must fill in the user input,
// but in case it doesn't, just use the keywords.
Expand Down Expand Up @@ -223,13 +224,16 @@ export async function inspect (page: Page, model: string, userInput: string, act
const run = await client.evaluate({
modelName: model,
instructions
}, {
BaseURL: modelProviderCredentials?.baseUrl,
APIKey: modelProviderCredentials?.apiKey
})

const output = (await run.text()).replace('\n', '').trim()
return [output]
}

export async function inspectForSelect (page: Page, model: string, userInput: string, userSelection: string, keywords?: string[]): Promise<{ selector: string, option: string }> {
export async function inspectForSelect (page: Page, modelProviderCredentials: ModelProviderCredentials | undefined, model: string, userInput: string, userSelection: string, keywords?: string[]): Promise<{ selector: string, option: string }> {
let elementData = ''
const modes = ['matchAll', 'oneSibling', 'twoSiblings', 'parent', 'grandparent']
for (const mode of modes) {
Expand Down Expand Up @@ -267,6 +271,9 @@ export async function inspectForSelect (page: Page, model: string, userInput: st
const run = await client.evaluate({
modelName: model,
instructions
}, {
BaseURL: modelProviderCredentials?.baseUrl,
APIKey: modelProviderCredentials?.apiKey
})

const output = (await run.text()).replace('\n', '').trim()
Expand Down
2 changes: 1 addition & 1 deletion browser/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function download(
const contentType = response.headers['content-type']
await client.writeFileInWorkspace(workspaceFile, Buffer.from(response.data), workspaceId)

return {
return {
url,
resolvedUrl: response.request.res.responseUrl || url,
contentType,
Expand Down
5 changes: 3 additions & 2 deletions browser/src/fill.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { delay } from './delay.ts'
import { type Page } from 'playwright'
import { inspect } from './browse.ts'
import { ModelProviderCredentials } from './session.ts'

export async function fill (page: Page, model: string, userInput: string, content: string, keywords: string[], matchTextOnly: boolean): Promise<void> {
const locators = await inspect(page, model, userInput, 'fill', matchTextOnly, keywords)
export async function fill (page: Page, modelProviderCredentials: ModelProviderCredentials | undefined, model: string, userInput: string, content: string, keywords: string[], matchTextOnly: boolean): Promise<void> {
const locators = await inspect(page, modelProviderCredentials, model, userInput, 'fill', matchTextOnly, keywords)
for (const locator of locators) {
console.log(locator)
try {
Expand Down
5 changes: 3 additions & 2 deletions browser/src/select.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type Page } from 'playwright'
import { inspectForSelect } from './browse.ts'
import { ModelProviderCredentials } from './session.ts'

export async function select (page: Page, model: string, userInput: string, option: string): Promise<void> {
const selection = await inspectForSelect(page, model, option, userInput)
export async function select (page: Page, modelProviderCredentials: ModelProviderCredentials | undefined, model: string, userInput: string, option: string): Promise<void> {
const selection = await inspectForSelect(page, modelProviderCredentials, model, option, userInput)

try {
await page.selectOption(`${selection.selector}`, selection.option, { timeout: 5000 })
Expand Down
4 changes: 3 additions & 1 deletion browser/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fill } from './fill.ts'
import { enter } from './enter.ts'
import { scrollToBottom } from './scrollToBottom.ts'
import { randomBytes } from 'node:crypto'
import { getSessionId, SessionManager } from './session.ts'
import { getSessionId, SessionManager, getModelProviderCredentials } from './session.ts'
import { screenshot, ScreenshotInfo } from './screenshot.ts'
import { download } from './download.ts'

Expand Down Expand Up @@ -58,6 +58,7 @@ async function main (): Promise<void> {
const keywords: string[] = (data.keywords ?? '').split(',')
const filter: string = data.filter ?? ''
const followMode: boolean = data.followMode === 'false' ? false : Boolean(data.followMode)
const modelProviderCredentials = getModelProviderCredentials(req.headers)

try {
const sessionID = getSessionId(req.headers)
Expand Down Expand Up @@ -100,6 +101,7 @@ async function main (): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
response.result = await fill(
page,
modelProviderCredentials,
model,
userInput,
data.content ?? '',
Expand Down
15 changes: 15 additions & 0 deletions browser/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ export function getWorkspaceId (headers: IncomingHttpHeaders): string | undefine
return getGPTScriptEnv(headers, 'GPTSCRIPT_WORKSPACE_ID')
}

export interface ModelProviderCredentials {
baseUrl: string
apiKey: string
}

export function getModelProviderCredentials(headers: IncomingHttpHeaders): ModelProviderCredentials | undefined {
const baseUrl = getGPTScriptEnv(headers, 'OPENAI_BASE_URL')?.trim()
if (!baseUrl) return undefined

const apiKey = getGPTScriptEnv(headers, 'OPENAI_API_KEY')?.trim()
if (!apiKey) return undefined

return { baseUrl, apiKey }
}

export function getGPTScriptEnv (headers: IncomingHttpHeaders, envKey: string): string | undefined {
const envHeader = headers?.['x-gptscript-env']
const envArray = Array.isArray(envHeader) ? envHeader : [envHeader]
Expand Down
5 changes: 4 additions & 1 deletion browser/tool.gpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Name: Browser
Description: Tools to navigate websites using a browser.
Metadata: bundle: true
Metadata: noUserAuth: sys.model.provider.credential
Credentials: github.com/gptscript-ai/credentials/model-provider
Share Tools: Browse, Get Page Contents, Filter, Fill, Enter, Scroll, Back, Forward, Screenshot, Download File From URL
Metadata: noUserAuth: sys.model.provider.credential

---
Name: Get Page Contents
Expand Down Expand Up @@ -43,6 +43,8 @@ Tools: service
---
Name: Fill
Metadata: index: false
Metadata: noUserAuth: sys.model.provider.credential
Credentials: github.com/gptscript-ai/credentials/model-provider
Share Context: Browser Context
Tools: service
Description: Fills text into an element on the web page. Useful for filling out forms and other input fields.
Expand Down Expand Up @@ -181,6 +183,7 @@ END TOOL USAGE: The `screenshotInfo` response field

---
Name: service
Metadata: requestedEnvVars: OPENAI_API_KEY,OPENAI_BASE_URL
Metadata: index: false

#!sys.daemon /usr/bin/env npm --prefix ${GPTSCRIPT_TOOL_DIR} run server
Expand Down
Loading
0