Skip to content

docs: Add python API guide #730

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

docs: Add python API guide #730

wants to merge 8 commits into from

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 Apr 17, 2025 3:17am

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

@jyecusch jyecusch requested a review from davemooreuws April 8, 2025 04:19
davemooreuws
davemooreuws previously approved these changes Apr 8, 2025
# Call OpenAI API to get summary (this is a blocking call, for demo simplicity)
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
Copy link
Member

Choose a reason for hiding this comment

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

should we allow this be to be configurable?

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
Copy link
Member

Choose a reason for hiding this comment

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

This seems very low, has this been tested?

Comment on lines +127 to +130
```python
# Create OpenAI client
client = OpenAI(api_key=api_key)
```
Copy link
Member

Choose a reason for hiding this comment

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

Could also provide the user the option of testing locally using ollama if they have it installed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants