Convert Figma logo to code with AI

openai logoopenai-cookbook

Examples and guides for using the OpenAI API

58,432
9,267
58,432
61

Top Related Projects

๐Ÿค— Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

93,526

๐Ÿฆœ๐Ÿ”— Build context-aware reasoning applications

Quick Overview

The openai/openai-cookbook repository is a collection of example code snippets and guides for working with OpenAI's APIs, particularly focusing on the GPT models. It serves as a practical resource for developers looking to integrate OpenAI's technologies into their projects, offering best practices and solutions to common use cases.

Pros

  • Comprehensive collection of examples covering various use cases
  • Regularly updated with new features and improvements
  • Well-documented code snippets and explanations
  • Contributions from both OpenAI staff and community members

Cons

  • Some examples may become outdated as the API evolves
  • Not a complete replacement for official documentation
  • May require additional context or knowledge for more complex implementations
  • Limited to Python examples, lacking coverage for other programming languages

Code Examples

  1. Simple completion example using the OpenAI API:
import openai

openai.api_key = "your-api-key-here"

response = openai.Completion.create(
  model="text-davinci-002",
  prompt="Translate the following English text to French: 'Hello, how are you?'",
  max_tokens=60
)

print(response.choices[0].text.strip())
  1. Using the ChatGPT API for a conversation:
import openai

openai.api_key = "your-api-key-here"

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}
]

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=messages
)

print(response.choices[0].message.content)
  1. Fine-tuning a model with custom data:
import openai

openai.api_key = "your-api-key-here"

# Prepare your training data file
training_file = openai.File.create(
  file=open("path/to/fine-tune-data.jsonl", "rb"),
  purpose='fine-tune'
)

# Create a fine-tuning job
fine_tune_job = openai.FineTune.create(
  training_file=training_file.id,
  model="davinci"
)

print(f"Fine-tuning job created with ID: {fine_tune_job.id}")

Getting Started

To get started with the OpenAI Cookbook:

  1. Clone the repository:

    git clone https://github.com/openai/openai-cookbook.git
    
  2. Install the OpenAI Python library:

    pip install openai
    
  3. Set up your API key as an environment variable:

    export OPENAI_API_KEY='your-api-key-here'
    
  4. Explore the examples in the repository and adapt them to your needs.

Competitor Comparisons

๐Ÿค— Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

Pros of transformers

  • Comprehensive library with support for multiple architectures and tasks
  • Extensive documentation and community support
  • Flexible and customizable for various NLP applications

Cons of transformers

  • Steeper learning curve for beginners
  • Requires more computational resources for training and fine-tuning
  • May be overkill for simple NLP tasks or API-based applications

Code comparison

transformers:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
result = classifier("I love this product!")[0]
print(f"Label: {result['label']}, Score: {result['score']:.4f}")

openai-cookbook:

import openai

response = openai.Completion.create(
  engine="text-davinci-002",
  prompt="Sentiment analysis: I love this product!",
  max_tokens=60
)
print(response.choices[0].text.strip())

The transformers example demonstrates a more straightforward approach for specific NLP tasks, while the openai-cookbook example showcases the flexibility of using API-based solutions for various language tasks. transformers offers more control and customization, whereas openai-cookbook provides a simpler interface for leveraging pre-trained models through an API.

93,526

๐Ÿฆœ๐Ÿ”— Build context-aware reasoning applications

Pros of langchain

  • More comprehensive framework for building LLM applications
  • Extensive integrations with various tools and services
  • Active community and frequent updates

Cons of langchain

  • Steeper learning curve due to its complexity
  • May be overkill for simple LLM-based projects
  • Potential overhead in terms of performance and dependencies

Code Comparison

openai-cookbook:

import openai

response = openai.Completion.create(
  engine="text-davinci-002",
  prompt="Translate the following English text to French: '{}'",
  max_tokens=60
)

langchain:

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate

llm = OpenAI(model_name="text-davinci-002")
prompt = PromptTemplate(
    input_variables=["text"],
    template="Translate the following English text to French: {text}"
)

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

OpenAI Cookbook Logo

รขยœยจ Navigate at cookbook.openai.com

Example code and guides for accomplishing common tasks with the OpenAI API. To run these examples, you'll need an OpenAI account and associated API key (create a free account here). Set an environment variable called OPENAI_API_KEY with your API key. Alternatively, in most IDEs such as Visual Studio Code, you can create an .env file at the root of your repo containing OPENAI_API_KEY=<your API key>, which will be picked up by the notebooks.

Most code examples are written in Python, though the concepts can be applied in any language.

For other useful tools, guides and courses, check out these related resources from around the web.

Contributing

The OpenAI Cookbook is a community-driven resource. Whether you're submitting an idea, fixing a typo, adding a new guide, or improving an existing one, your contributions are greatly appreciated!

Before contributing, read through the existing issues and pull requests to see if someone else is already working on something similar. That way you can avoid duplicating efforts.

If there are examples or guides you'd like to see, feel free to suggest them on the issues page.

If you'd like to contribute new content, make sure to read through our contribution guidelines. We welcome high-quality submissions of new examples and guides, as long as they meet our criteria and fit within the scope of the cookbook.

The contents of this repo are automatically rendered into cookbook.openai.com based on registry.yaml.

Open in GitHub Codespaces