Skip to content

Commit

Permalink
Merge branch 'main' into struct-op-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmer1 authored Nov 27, 2024
2 parents 4b66e06 + eb807b2 commit 6da8b83
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 5 deletions.
86 changes: 86 additions & 0 deletions .github/scripts/check_python_code_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import re
from pathlib import Path
import black

DEFAULT_LINE_LENGTH = 70
BASE_DIR = Path(__file__).resolve().parent
MDX_DIR = BASE_DIR / "../../fern/pages"
FILE_PATTERN = re.compile(r"\.mdx$")


def find_files_by_pattern(directory, pattern):
"""
Finds all files in the given directory that match the provided regex pattern.
"""
directory = Path(directory).resolve()
if not directory.is_dir():
raise ValueError(f"Provided directory {directory} is not valid.")
return [f for f in directory.rglob('*') if f.is_file() and pattern.search(f.name)]


def format_python_snippets_in_mdx(file_path, line_length=DEFAULT_LINE_LENGTH):
"""
Formats Python code snippets inside MDX files using Black.
"""
black_mode = black.FileMode(line_length=line_length)
code_block_pattern = re.compile(r"```python\n(.*?)\n```", re.DOTALL)

with open(file_path, 'r', encoding='utf-8') as file:
original_content = file.read()

def format_with_black(match):
code = match.group(1)
formatted_code = black.format_str(code, mode=black_mode)
return f"```python\n{formatted_code.strip()}\n```"

new_content = code_block_pattern.sub(format_with_black, original_content)

with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)

return original_content, new_content


def process_mdx_files(directory, file_pattern, line_length=DEFAULT_LINE_LENGTH, check_changes=False):
"""
Processes all MDX files in the directory, formatting Python code snippets.
Args:
directory (Path or str): Path to the directory containing MDX files.
file_pattern (re.Pattern): Regex pattern to match MDX files.
line_length (int): Line length to use for Black formatting.
check_changes (bool): If True, raises an exception if changes are detected.
"""
matching_files = find_files_by_pattern(directory, file_pattern)
files_changed = []

for file_path in matching_files:
original_content, new_content = format_python_snippets_in_mdx(file_path, line_length)

if original_content != new_content:
files_changed.append(file_path)

if check_changes and files_changed:
raise RuntimeError(
f"The following files were modified during the run:\n"
+ "\n".join(str(file) for file in files_changed)
)


if __name__ == "__main__":
import sys

path = sys.argv[1] if len(sys.argv) > 1 else MDX_DIR
line_length = int(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_LINE_LENGTH
check_changes = os.getenv("CI") == "true" # Set to True in CI pipeline

if Path(path).is_dir():
process_mdx_files(path, FILE_PATTERN, line_length, check_changes)
elif Path(path).is_file():
if FILE_PATTERN.search(path):
process_mdx_files(Path(path).parent, FILE_PATTERN, line_length, check_changes)
else:
print("The specified file does not match the MDX pattern.")
else:
print("Provided path is not valid.")
39 changes: 39 additions & 0 deletions .github/workflows/check-python-code-snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: check-python-code-snippets

on:
pull_request:
branches:
- main
paths:
- 'fern/pages/**/*.mdx'
- 'fern/pages/**/**/*.mdx'

jobs:
run:
runs-on: ubuntu-latest
permissions: write-all

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install Poetry
shell: bash
run: |
pipx install poetry
- name: Install Project Dependencies with Poetry
shell: bash
run: |
poetry install
- name: Run Python MDX Snippet Formatter
shell: bash
env:
CI: true
run: poetry run python .github/scripts/check_python_code_snippets.py fern/pages
11 changes: 10 additions & 1 deletion cohere-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5962,6 +5962,15 @@ paths:
A list of available tools (functions) that the model may suggest invoking before producing a text response.

When `tools` is passed (without `tool_results`), the `text` content in the response will be empty and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty.
strict_tools:
x-fern-audiences:
- public
x-fern-availability: beta
type: boolean
description: |
When set to `true`, tool calls in the Assistant message will be forced to follow the tool definition strictly. Learn more in the [Strict Tools guide](https://docs.cohere.com/docs/structured-outputs-json#structured-outputs-tools).

**Note**: The first few requests with a new set of tools will take longer to process.
documents:
type: array
items:
Expand Down Expand Up @@ -6037,7 +6046,7 @@ paths:
minimum: 0
maximum: 500
description: |
Ensures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled.
Ensures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled.
Defaults to `0`, min value of `0`, max value of `500`.
p:
type: number
Expand Down
4 changes: 2 additions & 2 deletions fern/pages/models/aya.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Aya Family of Models
slug: aya/tools
slug: "docs/aya"
hidden: false
description: >-
Understand Cohere for AI's groundbreaking multilingual Aya models, which aim to bring many more languages into generative AI.
Expand Down Expand Up @@ -66,7 +66,7 @@ Espero que esta historia te sea útil para ilustrar vocabulario sencillo en espa
Finally, you can directly download the raw models for research purposes because Cohere For AI has released [Aya Expanse 8B](https://huggingface.co/CohereForAI/aya-expanse-8b) and [Aya Expanse 32B](https://huggingface.co/CohereForAI/aya-expanse-32b) as open-weight models, through HuggingFace. What’s more, the massively multilingual instruction data used for development of these models has been [made available](https://huggingface.co/datasets/CohereForAI/aya_collection) for download as well.

## Find More
We hope you’ve found this as fascinating as we do! If you want to see more substantial projects you can check out these notebooks (source):
We hope you’ve found this as fascinating as we do! If you want to see more substantial projects you can check out these notebooks ([source](https://huggingface.co/CohereForAI/aya-expanse-32b)):

- [Multilingual Writing Assistant](https://colab.research.google.com/drive/1SRLWQ0HdYN_NbRMVVUHTDXb-LSMZWF60)
- [AyaMCooking](https://colab.research.google.com/drive/1-cnn4LXYoZ4ARBpnsjQM3sU7egOL_fLB?usp=sharing)
Expand Down
100 changes: 98 additions & 2 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
cohere = "^5.11.1"
black = "^24.10.0"


[build-system]
Expand Down

0 comments on commit 6da8b83

Please sign in to comment.