Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

docs: Add python API guide #730

Merged
merged 12 commits into from
May 9, 2025
Merged

docs: Add python API guide #730

merged 12 commits into from
May 9, 2025

Conversation

jyecusch
Copy link
Member

@jyecusch jyecusch commented Apr 2, 2025

No description provided.

Copy link

vercel bot commented Apr 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nitric-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 8, 2025 4:11am

Copy link
Member

@davemooreuws davemooreuws left a comment

Choose a reason for hiding this comment

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

some spellcheck issues

@raksiv
Copy link
Member

raksiv commented Apr 3, 2025

Tested it with an OpenAPI Key -

{
"error": "\n\nYou tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.\n\nYou can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface. \n\nAlternatively, you can pin your installation to the old version, e.g. pip install openai==0.28\n\nA detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742\n"
}

Updated code:

import os
from openai import OpenAI
from nitric.resources import api
from nitric.application import Nitric
from nitric.context import HttpContext

# Configure OpenAI API key from env
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
  raise Exception("Please set the OPENAI_API_KEY environment variable")

# Create OpenAI client
client = OpenAI(api_key=api_key)

# Define Nitric API
summarize_api = api("main")

@summarize_api.post("/summarize")
async def summarize_text(ctx: HttpContext) -> None:
  req_data = ctx.req.json
  if not req_data or 'text' not in req_data:
    ctx.res.status = 400
    ctx.res.body = {"error": "No text provided for summarization."}
    return

  text_to_summarize = req_data['text']
  try:
    response = client.chat.completions.create(
      model="gpt-3.5-turbo",
      messages=[{"role": "user", "content": f"Summarize the following text:\n\n{text_to_summarize}"}],
      max_tokens=50
    )
    summary = response.choices[0].message.content
    ctx.res.body = {"summary": summary.strip()}
  except Exception as e:
    ctx.res.status = 500
    ctx.res.body = {"error": str(e)}

Nitric.run()

Copy link
Member

@raksiv raksiv left a comment

Choose a reason for hiding this comment

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

Update code to deal with deprecations.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

Files not reviewed (2)
  • dictionary.txt: Language not supported
  • docs/guides/python/serverless-ai-api.mdx: Language not supported

davemooreuws
davemooreuws previously approved these changes Apr 8, 2025
@jyecusch jyecusch merged commit e2ef5ff into main May 9, 2025
6 checks passed
@jyecusch jyecusch deleted the docs/python-ai-tutorial branch May 9, 2025 02:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants