-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some spellcheck issues
Tested it with an OpenAPI Key - { 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() |
There was a problem hiding this 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.
There was a problem hiding this 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
# 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", |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
```python | ||
# Create OpenAI client | ||
client = OpenAI(api_key=api_key) | ||
``` |
There was a problem hiding this comment.
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
Co-authored-by: Ryan Cartwright <[email protected]>
No description provided.