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

OpenAI flavor #8155

Merged
merged 12 commits into from Apr 17, 2023
Merged

OpenAI flavor #8155

merged 12 commits into from Apr 17, 2023

Conversation

harupy
Copy link
Member

@harupy harupy commented Apr 3, 2023

Related Issues/PRs

#xxx

What changes are proposed in this pull request?

(Please fill in changes proposed in this fix)

How is this patch tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests (describe details, including test results, below)
    • with Azure OpenAI (using a real API key)
    • with OpenAI (using a real API key)

Does this PR change the documentation?

  • No. You can skip the rest of this section.
  • Yes. Make sure the changed pages / sections render correctly in the documentation preview.

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

(Details in 1-2 sentences. You can just refer to another PR with a description if this PR is part of a larger change.)

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/recipes: Recipes, Recipe APIs, Recipe configs, Recipe Templates
  • area/projects: MLproject format, project running backends
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/server-infra: MLflow Tracking server backend
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

@mlflow-automation
Copy link
Collaborator

mlflow-automation commented Apr 3, 2023

Documentation preview for 357f841 will be available here when this CircleCI job completes successfully.

More info

@harupy harupy changed the title OpenAI flavor [DO NOT REVIEW YET] OpenAI flavor Apr 3, 2023
@github-actions github-actions bot added area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs. labels Apr 6, 2023
:param path: Local filesystem path to the MLflow Model with the ``openai`` flavor.
"""
wrapper_cls = _TestOpenAIWrapper if _MLFLOW_OPENAI_TESTING.get() else _OpenAIWrapper
return wrapper_cls(_load_model(path))
Copy link
Member Author

Choose a reason for hiding this comment

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

Could not find a good way to mock requests in the UDF

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense :)

@harupy harupy changed the title [DO NOT REVIEW YET] OpenAI flavor OpenAI flavor Apr 6, 2023
mlflow/openai/__init__.py Outdated Show resolved Hide resolved
@harupy harupy force-pushed the openai-flavor branch 2 times, most recently from 8e00ffd to 70af162 Compare April 8, 2023 00:17
@harupy harupy marked this pull request as ready for review April 10, 2023 03:38
Copy link
Collaborator

@jinzhang21 jinzhang21 left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@BenWilson2 BenWilson2 left a comment

Choose a reason for hiding this comment

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

LGTM! Great work @harupy

mlflow/openai/__init__.py Outdated Show resolved Hide resolved
@harupy harupy force-pushed the openai-flavor branch 2 times, most recently from 6934854 to bee4e70 Compare April 13, 2023 09:33
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
_save_example(mlflow_model, input_example, path)
if metadata is not None:
mlflow_model.metadata = metadata
model_data_subpath = "model.json"
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
model_data_subpath = "model.json"
model_data_subpath = "model.yaml"

@jinzhang21 maybe we should use yaml here as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, but @sunishsheth2009 mentioned it doesn't work with YAML because Langchain doesn't serialize / format it properly. Does it work with OpenAI? I'd prefer to use YAML for consistency here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Langchain doesn't serialize / format it properly

@sunishsheth2009 Can you elaborate on this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes!! So in Langchain, I think its a bug with Langchain, but this is how it stores the yaml file

_type: !!python/object/apply:langchain.agents.agent_types.AgentType
- zero-shot-react-description
allowed_tools:
- Search
- Calculator
llm_chain:
  _type: llm_chain
  llm:
    _type: openai
    best_of: 1
    frequency_penalty: 0
    logit_bias: {}
    max_tokens: 256
    model_name: text-davinci-003
    n: 1
    presence_penalty: 0
    request_timeout: null
    temperature: 0.0
    top_p: 1
  memory: null
  output_key: text
  prompt:
    _type: prompt
    input_variables:
    - input
    - agent_scratchpad
    output_parser: null
    partial_variables: {}
    template: 'Answer the following questions as best you can. You have access to
      the following tools:

but json is stored like this:

{
  "llm_chain": {
    "memory": null,
    "verbose": false,
    "prompt": {
      "input_variables": [
        "input",
        "agent_scratchpad"
      ],
      "output_parser": null,
      "partial_variables": {},
      "template": "Answer the following questions as best you can. You have access to the following tools:\n\nSearch: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.\nCalculator: Useful for when you need to answer questions about math.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Search, Calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: {input}\nThought:{agent_scratchpad}",
      "template_format": "f-string",
      "validate_template": true,
      "_type": "prompt"
    },
    "llm": {
      "model_name": "text-davinci-003",
      "temperature": 0,
      "max_tokens": 256,
      "top_p": 1,
      "frequency_penalty": 0,
      "presence_penalty": 0,
      "n": 1,
      "best_of": 1,
      "request_timeout": null,
      "logit_bias": {},
      "_type": "openai"
    },
    "output_key": "text",
    "_type": "llm_chain"
  },
  "allowed_tools": [
    "Search",
    "Calculator"
  ],
  "_type": "zero-shot-react-description"
}

See that the type is incorrect in yaml but it is correct in json.
Maybe we can create a bug report with Langchain if we decide to go yaml with Langchain Agent. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

_type: !!python/object/apply:langchain.agents.agent_types.AgentType

does seem incorrect.

Copy link
Member Author

Choose a reason for hiding this comment

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

Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
Signed-off-by: harupy <hkawamura0130@gmail.com>
@harupy harupy merged commit 6a33d1d into mlflow:master Apr 17, 2023
25 of 26 checks passed
@harupy harupy mentioned this pull request Apr 17, 2023
33 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants