Skip to content
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

CLI: apollo command #682

Merged
merged 12 commits into from
May 28, 2024
Merged

CLI: apollo command #682

merged 12 commits into from
May 28, 2024

Conversation

josephjclark
Copy link
Collaborator

@josephjclark josephjclark commented May 2, 2024

Short Description

This PR adds a CLI command to call out to the new Apollo Server

This is how I expect most consumers to use the AI services being developed over in gen.

Some services, like adaptor generation, are likely to get a dedicated command, with a simpler interface and more help.

Apollo logs are streamed into the CLI via websocket connection.

The CLI recognises the { files } key in results and will either nicely log the files to stdout out write the files to disk, according to to input parameters.

Since this is bleeding edge functionality I only intend to add very light test coverage

Related issue

Closes #680

Basic Usage

The basic usage format is:

openfn apollo <service-name> path/to/input.json

By default the CLI will call out to the staging server (which obviously doesn't exist yet). But you can pass --local to use a local server on the default port, or set OPENFN_APOLLO_DEFAULT_ENV=local to default to using a local server.

Output will be logged to stdout by default, or you can pas -o and set the output path, just like other services.

Sample output

$ openfn apollo adaptor_gen tmp/adaptor-cat-fact.json --local  

CLI] ♦ Calling Apollo service: adaptor_gen
[CLI] ✔ Using apollo server at http://localhost:3000
[CLI] ♦ Calling apollo:  ws://localhost:3000/services/adaptor_gen
[APO] ℹ None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
[APO] ℹ INFO:adaptor_gen:Generating adaptor template for /facts
[APO] ℹ INFO:adaptor_gen:prompt: Create an OpenFn function that accesses the //facts endpoint
[APO] ℹ INFO:signature_generator:Generating signature for model gpt3_turbo
[APO] ℹ INFO:signature_generator:Parsing OpenAPI specification
[APO] ℹ INFO:signature_generator:Extracting API information from parsed spec with provided instruction
[APO] ℹ INFO:signature_generator:Generating gpt3_turbo prompt for signature generation
[APO] ℹ INFO:inference.gpt3_turbo:OpenAI GPT-3.5 Turbo client loaded.
[APO] ℹ INFO:inference.gpt3_turbo:Generating
[APO] ℹ INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
[APO] ℹ INFO:inference.gpt3_turbo:done
[APO] ℹ INFO:signature_generator:Signature generation complete
[APO] ℹ INFO:code_generator.prompts:Generating prompt for: code
[APO] ℹ INFO:code_generator.prompts:Prompt generation complete for: code
[APO] ℹ INFO:inference.gpt3_turbo:OpenAI GPT-3.5 Turbo client loaded.
[APO] ℹ INFO:inference.gpt3_turbo:Generating
[APO] ℹ INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
[APO] ℹ INFO:inference.gpt3_turbo:done
[CLI] ✔ Result:
-------------
Adaptor.d.ts
-------------
/**
  * Retrieves facts from the //facts endpoint and includes them in the state data.
  * Sends a GET request to the //facts endpoint.
  * @example
  * getFacts(callback)
  * @function
  * @param {Function} callback - A callback which is invoked with the resulting state at the end of this operation. Allows users to customize the resulting state. State.data includes the response from the //facts endpoint.
  * @example <caption>Get facts from the //facts endpoint</caption>
  * getFacts()
  * @returns {Function} A function that updates the state with the retrieved facts.
  */
  export function getFacts(callback?: Function): Operation;
-----------
Adaptor.js
-----------
```javascript
import { http } from '@openfn/language-common';
/**
 * Retrieves facts from the //facts endpoint and includes them in the state data.
 * Sends a GET request to the //facts endpoint.
 * @example
 * getFacts(callback)
 * @function
 * @param {Function} callback - A callback which is invoked with the resulting state at the end of this operation. Allows users to customize the resulting state. State.data includes the response from the //facts endpoint.
 * @example <caption>Get facts from the //facts endpoint</caption>
 * getFacts()
 * @returns {Function} A function that updates the state with the retrieved facts.
 */
export function getFacts(callback?: Function): Operation {
  return async (state) => {
    try {
      const response = await http.get('//facts');
      const data = response.data;
      const newState = { ...state, data };
      if (callback) {
        return callback(newState);
      }
      return newState;
    } catch (error) {
      console.error('Error retrieving facts:', error);
      return state;
    }
  };
}
```

Future Work

Maybe in this PR, maybe not?

  • openfn apollo list should list available services

  • openfn apollo help <service-name> should print help for a specific service (probably the readme)

  • Stream logs back to the CLI via websocket

@josephjclark josephjclark changed the title CLI: add an apollo command CLI: apollo command May 22, 2024
@@ -0,0 +1,189 @@
{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've deliberately checked this in so that we have some kind of test data for template gen. I'm not wild about it, it's a bit of a knee-jerk decision really

@josephjclark josephjclark changed the base branch from main to release/next May 28, 2024 10:41
@josephjclark josephjclark mentioned this pull request May 28, 2024
6 tasks
@josephjclark josephjclark merged commit 015055c into release/next May 28, 2024
5 checks passed
@josephjclark josephjclark deleted the cli-apollo branch May 28, 2024 10:57
@josephjclark josephjclark mentioned this pull request May 28, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

CLI: apollo command
1 participant