This message was deleted.
# community-help
s
This message was deleted.
j
Here's a guide on how to build this: https://typesense.org/docs/guide/recommendations.html To adapt that to your use-case, I would recommend extracting key topics from each question like "jeans", "t-shirt", "q1 2023", "2022", etc and treat each as an entity. As each user interacts, you want to add a new record to the training dataset with just the keywords.
c
Copy code
const fs = require('fs');
const _ = require('lodash')
const Typesense = require('typesense')

const client = new Typesense.Client({
  'nodes': [{
    'host': 'localhost',
    'port': '8108',
    'protocol': 'http'
  }],
  'apiKey': 'xyz',
  'connectionTimeoutSeconds': 300
})

const schema = {
  'name': 'test',
  'fields': [
    { name: 'title', type: 'string', locale: 'zh' },
    { name: 'tags', type: 'string[]', locale: 'zh', optional: true },
    {
      name: 'embedding',
      type: 'float[]',
      embed: {
        from: [
          'title',
          'tags'
        ],
        model_config: {
          model_name: 'ts/all-MiniLM-L12-v2'
        }
      }
    }
  ]
}

;(async function () {
  try {
    await client.collections('test').delete()
  } catch {

  }

  try {
    await client.collections().create(schema)
  } catch (ex) {
    console.log(ex)
  }

  try {
    await client.collections('test').documents().import([
      { id: '1', title: '2023第一季度的牛仔裤销量是多少', tags: ['牛仔裤'] },
      { id: '2', title: '如何在2023年夏季保护皮肤免受阳光伤害', tags: ['皮肤保护', '夏季'] },
      { id: '3', title: '2023年最受欢迎的电影是哪部', tags: ['电影', '娱乐'] },
      { id: '4', title: '如何提高2023年第一季度的销售业绩', tags: ['销售', '业绩提升'] },
      { id: '5', title: '2023年最新的时尚趋势是什么', tags: ['时尚', '时装'] },
      { id: '6', title: '如何选择适合的健身计划来保持健康', tags: ['健身', '健康'] },
      { id: '7', title: '2023年哪些旅游目的地值得一去', tags: ['旅游', '目的地'] },
      { id: '8', title: '如何在2023年第一季度提高工作效率', tags: ['工作效率', '提升'] },
      { id: '9', title: '2023年最佳的手机摄影技巧有哪些', tags: ['手机摄影', '技巧'] },
      { id: '10', title: '如何在2023年第一季度管理个人财务', tags: ['个人财务', '理财'] },
      { id: '11', title: '2023年最受欢迎的音乐流派是什么', tags: ['音乐', '流派'] },
      { id: '12', title: '如何在2023年第一季度改善睡眠质量', tags: ['睡眠', '健康'] },
      { id: '13', title: '2023年最新的科技趋势有哪些', tags: ['科技', '趋势'] },
      { id: '14', title: '如何在2023年第一季度提高团队合作效果', tags: ['团队合作', '效果提升'] },
      { id: '15', title: '2023年最适合种植的室内植物有哪些', tags: ['室内植物', '种植'] },
      { id: '16', title: '如何在2023年第一季度学习一门新的技能', tags: ['学习', '技能'] },
      { id: '17', title: '2023年最新的健康饮食趋势有哪些', tags: ['健康饮食', '趋势'] },
      { id: '18', title: '如何在2023年第一季度改善家庭关系', tags: ['家庭关系', '改善'] },
      { id: '19', title: '2023年最新的游戏发布有哪些值得期待', tags: ['游戏', '发布'] },
      { id: '20', title: '如何在2023年第一季度提高个人品牌影响力', tags: ['个人品牌', '影响力提升'] },
      { id: '21', title: '2023年最适合的户外运动是什么', tags: ['户外运动', '健康'] },
      { id: '22', title: '如何在2023年第一季度提高创造力', tags: ['创造力', '提升'] },
      { id: '23', title: '2023年最新的汽车科技有哪些', tags: ['汽车', '科技'] },
      { id: '24', title: '如何在2023年第一季度养成健康的饮食习惯', tags: ['饮食习惯', '健康'] },
      { id: '25', title: '2023年最热门的社交媒体平台是哪个', tags: ['社交媒体', '平台'] },
      { id: '26', title: '如何在2023年第一季度建立有效的时间管理策略', tags: ['时间管理', '策略'] },
      { id: '27', title: '2023年最新的家居装饰趋势有哪些', tags: ['家居装饰', '趋势'] },
      { id: '28', title: '如何在2023年第一季度提高团队沟通效果', tags: ['团队沟通', '效果提升'] },
      { id: '29', title: '2023年最适合的假期目的地是哪里', tags: ['假期', '目的地'] },
      { id: '30', title: '如何在2023年第一季度养成阅读习惯', tags: ['阅读', '习惯养成'] },
      { id: '31', title: '2023年最新的健身器材有哪些推荐', tags: ['健身器材', '推荐'] },
      { id: '32', title: '如何在2023年第一季度管理工作与生活的平衡', tags: ['工作与生活平衡', '管理'] },
      { id: '33', title: '2023年最受欢迎的电子设备是什么', tags: ['电子设备', '科技'] },
      { id: '34', title: '如何在2023年第一季度提高个人效能', tags: ['个人效能', '提升'] },
      { id: '35', title: '2023年最新的美容护肤技巧有哪些', tags: ['美容护肤', '技巧'] },
      { id: '36', title: '如何在2023年第一季度规划旅行预算', tags: ['旅行预算', '规划'] },
      { id: '37', title: '2023年最受欢迎的社交活动是什么', tags: ['社交活动', '娱乐'] },
      { id: '38', title: '如何在2023年第一季度改善心理健康', tags: ['心理健康', '改善'] },
      { id: '39', title: '2023年最新的家用电器推荐有哪些', tags: ['家用电器', '推荐'] },
      { id: '40', title: '如何在2023年第一季度提高投资回报率', tags: ['投资', '回报率提升'] },
    ])
  } catch (ex) {
    console.log(ex)
  }

  const resp = await client.collections('test').documents().search({
    q: '电影',
    query_by: 'embedding,title,tags',
    // sort_by: '_vector_distance:desc'
  }).catch(console.log)

  const result = _.map(resp.hits, (item) => {
    // console.log(item.vector_distance)
    return {
      title: item.document.title,
      tags: item.document.tags,
      distance: item.vector_distance,
    }
  })

  console.log(JSON.stringify(result, null, 2))

  // console.log(await client.collections().retrieve())
} ())
this is the code for now users should save their tags(keywords) and how to query with many keywords?
Copy code
const resp = await client.collections('test').documents().search({
    q: 'movie,tag1,tag2,tag3...', // can i do this?
    query_by: 'embedding,title,tags',
    // sort_by: '_vector_distance:desc'
  }).catch(console.log)
j
The code you shared above is for doing semantic search on existing data - not for doing recommendations.
They both use vector search under the hood, but they are different use-cases
c
Is the training dataset used to obtain embedding values? Can we directly use ts/all-MiniLM-L12-v2 to get embeddings, and then query?
j
Is the training dataset used to obtain embedding values?
That's correct.
Can we directly use ts/all-MiniLM-L12-v2 to get embeddings, and then query?
ts/all-MiniLM-L12-v2
was trained on Wikipedia data. So if you use those embeddings, you'll get semantically similar results (based on language similarity). In your case, you mentioned that you wanted results based on user behavior on your site. So the model needs to be trained on this information to get recommendations
👀 1
❤️ 1
c
ts/all-MiniLM-L12-v2
was trained on Wikipedia data.
if i change embeding model to openai, will i get better embeding for demo purpose by not training? the title and tags field will generate from openai
To adapt that to your use-case, I would recommend extracting key topics from each question like "jeans", "t-shirt", "q1 2023", "2022", etc and treat each as an entity.
then i use keywords list from user like tag1 tag2 tag3 tag4 ... to get embeding from openai query with this api
Copy code
let searchRequests = {
  'searches': [
    {
      'collection': 'test',
      'q': '*',
      'vector_query' : 'embedding:([0.96826, 0.94, 0.39557, 0.306488], k:100)'
    }
  ]
}
let commonSearchParams = {}
client.multiSearch.perform(searchRequests, commonSearchParams)
so title has nothing to do here, all recommends related to tags right?
j
if i change embeding model to openai, will i get better embeding for demo purpose by not training?
OpenAI models are also language-based models, trained on public web content. So you'll only get linguistically related results, and that won't take user behavior on your site into account
so title has nothing to do here, all recommends related to tags right?
That's right. It's more like entities - they can be anything. What the model will give you is given these set of entity patterns in the past, given a new pattern, what is the next entity in this sequence
c
OpenAI models are also language-based models, trained on public web content. So you'll only get linguistically related results, and that won't take user behavior on your site into account
We're using the
product_name
in this example to make this article easier to read. In a production setting, you'd want to use the product's ID or SKU in the training dataset.
the image's marked part is something what i want to recommend, its mainly questions and statements to guide user query database. there's no sku or product's id, just like data in the code above.
I'm having a bit of trouble describing this clearly. I'm not quite sure I fully understand now. The user interaction I'm imagining is that the user views "questions". These "questions" will have keywords extracted from them? Then the recommendations are based on these keywords?
As users continuously interact, the interactions result in keywords being extracted from the "questions". These keywords will be made into a string separated by spaces, then embeddings will be obtained for this string. The embeddings will be used to search using nearest-neighbor vector search.
j
Question: do you want to do something like "other users who've viewed X, Y, Z, also viewed A, so I'm going to recommend A to this new user"?
c
ohoh yes
j
Because in this case, it's only a single user's interaction that's determining what they get shown. And it doesn't take into account other user's interaction, to recommend content to the user
❤️ 1
If you don't need other users' interactions to influence what a user gets shown, then you can use any of the linguistic models like OpenAI / mini-lm, etc. But if you need other users' interactions to be taken into account for a new user, then you'd need more than a linguistic model
c
something like graph db neo4j ?
j
But if you need other users' interactions to be taken into account for a new user, then you'd need more than a linguistic model
.... I meant to say, something like the starspace model described in that guide article I shared above.
there's no sku or product's id, just like data in the code above
In your case, instead of SKU / productIDs, you'd may be use question IDs for eg. It can really be any string
c
ok thanks, let me read carefuly about recommends guide many thanks🙏
👍 1
Oh, I think I understand why now. For example: User A likes: movies, music, and eating. User B likes: eating, dressing up, and music. The "dress" preference might be displayed to User A.
so it can be id, they just relate to each other use id to get original question
j
Yup exactly!
So in the case of recommendations we're defining "near-ness" based on all users' behavior. In the case of language models like OpenAI, mini-lm, etc they've defined "near-ness" only based on linguistic similarity between words
💯 1