跳至內容
選單
此問題已被標幟
2 回覆
945 瀏覽次數

Hi everyone,

I'm currently working on a project with Odoo 12 Community, and I would like to integrate OpenAI's API (e.g., to generate reports or summaries based on notes in models).

However, I'm facing a compatibility issue:

Odoo 12 uses Python 3.5, but the official openai Python library now requires Python 3.6 or higher.

Has anyone successfully used the OpenAI API with Odoo 12?

What would be the best approach to make it work?

  • Is there a way to upgrade Python in an Odoo 12 environment without breaking compatibility? 

Any advice, examples, or best practices would be greatly appreciated!

Thanks in advance.

頭像
捨棄
作者

👋 Hi everyone,

Huge thanks to @Piyush H and @Desk Enterprise for your clear and helpful answers!

✅ I followed your advice and used direct HTTP requests with requests, which works perfectly with Odoo 12 (Python 3.5).

This approach is clean, effective, and fully compatible—no need to upgrade Python or use the official OpenAI library.

🔥 For anyone wondering: using the API directly without the openai package is a great workaround for legacy environments.

Thanks again and best of luck to all of you!

Amazigh

最佳答案

Hii,

Use direct HTTP requests instead of the official openai Python library.

You can integrate OpenAI API using requests, which works with Python 3.5.
Example:
import requests

import json


def call_openai(prompt):

    headers = {

        "Authorization": "Bearer YOUR_OPENAI_API_KEY",

        "Content-Type": "application/json",

    }

    data = {

        "model": "gpt-3.5-turbo",

        "messages": [{"role": "user", "content": prompt}],

        "temperature": 0.7,

    }

    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, data=json.dumps(data))

    result = response.json()

    return result['choices'][0]['message']['content']

 i Hope It Is help full

頭像
捨棄
最佳答案

Since Odoo 12 runs on Python 3.5 and the official openai package requires Python 3.6+, here are your best options:

Option 1: Use OpenAI API Directly (Recommended)

Instead of using the official Python package, make direct HTTP requests to the OpenAI API:

  1. Install requests package (works with Python 3.5):
    bash
    pip install requests
  2. Create a custom module with this basic implementation:
    python
    import requests
    import json
    from odoo import models, fields, api
    
    class OpenAIIntegration(models.Model):
        _name = 'openai.integration'
    
        def call_openai(self, prompt):
            api_key = "your-api-key"  # Store this securely in config parameters
            headers = {
                "Authorization": f"Bearer {api_key}",
                "Content-Type": "application/json"
            }
            data = {
                "model": "text-davinci-003",  # or newer model
                "prompt": prompt,
                "temperature": 0.7,
                "max_tokens": 256
            }
            
            try:
                response = requests.post(
                    "https://api.openai.com/v1/completions",
                    headers=headers,
                    data=json.dumps(data)
                return response.json().get('choices')[0].get('text')
            except Exception as e:
                return f"Error: {str(e)}"
Option 2: Use Older OpenAI Package Version

Try version 0.10.8 (last version supporting Python 3.5):

bash

pip install openai==0.10.8
Option 3: Docker Workaround (Advanced)

Run a separate Python 3.6+ microservice that handles OpenAI calls and communicates with Odoo via:

  • REST API
  • XML-RPC
  • Message queue (RabbitMQ)
Implementation Example

To generate report summaries from notes:

python

@api.model
def generate_summary(self, note_id):
    note = self.env['note.note'].browse(note_id)
    prompt = f"Summarize this business note in 3 bullet points:\n\n{note.name}"
    summary = self.call_openai(prompt)
    note.write({'summary': summary})
Important Notes
  1. Security: Never hardcode API keys - use ir.config_parameter
  2. Error Handling: Add proper timeouts and retries
  3. Rate Limits: Implement throttling (OpenAI has strict limits)
  4. Cost Control: Monitor token usage to avoid surprise bills

Alternative Approach

Consider upgrading to Odoo 13+ (Python 3.6+) if possible, as this will give you:

  • Better Python version support
  • Official OpenAI package compatibility

Security updates

🚀 Did This Solve Your Problem?

If this answer helped you save time, money, or frustration, consider:

✅ Upvoting (👍) to help others find it faster

✅ Marking as "Best Answer" if it resolved your issue

Your feedback keeps the Odoo community strong! 💪

(Need further customization? Drop a comment—I’m happy to refine the solution!)

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
12月 24
908
2
3月 25
1120
0
6月 24
1276
1
7月 25
79
0
7月 25
156