Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

appv4.py 2.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  1. from flask import Flask, request, jsonify
  2. import openai
  3. import json
  4. from pinecone import Pinecone
  5. import os
  6. app = Flask(__name__)
  7. # Define the system prompt
  8. system_message = """
  9. You have been assigned the task of processing user requests presented in natural language and converting them into structured data for further analysis.
  10. Instructions:
  11. 1. Identify and extract entities (like names, organizations, products, etc.) from the user's request ($request). Store these entities in a variable called entities.
  12. 2. Identify the user's intentions behind $request. Extract these intentions and store them in a variable named intents.
  13. 3. Analyze $request to find synonyms and linguistic variations. Create a normalized version of the query that maintains the original request's meaning in a standardized form. Store this normalized query in normalized_query.
  14. 4. Create an array named normalized_queries consisting of at least five different rephrasings of $request. Each variant should represent the same intents but with different synonyms and tones.
  15. 5. Ensure the result is formatted as a JSON string only, containing no extra text or formatting.
  16. """
  17. @app.route('/llm/generate', methods=['POST'])
  18. def generate_response():
  19. user_query = request.get_json().get('query')
  20. # Set your OpenAI API key
  21. openai.api_key = os.environ.get('OPENAI_API_KEY')
  22. # Send the chat completion request
  23. response = openai.ChatCompletion.create(
  24. model="gpt-4",
  25. messages=[
  26. {"role": "system", "content": system_message},
  27. {"role": "user", "content": user_query}
  28. ],
  29. temperature=0.0
  30. )
  31. response_text = response.choices[0].message.content
  32. response_data = json.loads(response_text)
  33. # Your remaining code here
  34. return jsonify({'matches': matches})
  35. if __name__ == '__main__':
  36. app.run(debug=True)
  37. # curl -X POST http://localhost:5000/llm/generate -H 'Content-Type: application/json' -d '{"query": "I want to buy a red t-shirt"}'
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...